本文整理汇总了C#中UnicodeEncoding.GetBytes方法的典型用法代码示例。如果您正苦于以下问题:C# UnicodeEncoding.GetBytes方法的具体用法?C# UnicodeEncoding.GetBytes怎么用?C# UnicodeEncoding.GetBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnicodeEncoding
的用法示例。
在下文中一共展示了UnicodeEncoding.GetBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: toByteArray
public byte[] toByteArray()
{
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] player_bytes = encoding.GetBytes(player);
byte[] description_bytes = encoding.GetBytes(description);
byte[] bytes = new byte[12 + player_bytes.Length + description_bytes.Length + image.Length];
int array_index = 0;
KLFCommon.intToBytes(index).CopyTo(bytes, array_index);
array_index += 4;
KLFCommon.intToBytes(player_bytes.Length).CopyTo(bytes, array_index);
array_index += 4;
player_bytes.CopyTo(bytes, array_index);
array_index += player_bytes.Length;
KLFCommon.intToBytes(description_bytes.Length).CopyTo(bytes, array_index);
array_index += 4;
description_bytes.CopyTo(bytes, array_index);
array_index += description_bytes.Length;
image.CopyTo(bytes, array_index);
return bytes;
}
示例2: Start
// Use this for initialization
private void Start()
{
int count;
byte[] byteArray;
char[] charArray;
UnicodeEncoding uniEncoding = new UnicodeEncoding();
// Create the data to write to the stream.
byte[] firstString = uniEncoding.GetBytes(
"Invalid file path characters are: ");
byte[] secondString = uniEncoding.GetBytes(
"123456789");
using (MemoryStream memStream = new MemoryStream(100))
{
// Write the first string to the stream.
memStream.Write(firstString, 0, firstString.Length);
// Write the second string to the stream, byte by byte.
count = 0;
while (count < secondString.Length)
{
memStream.WriteByte(secondString[count++]);
}
// Write the stream properties to the console.
Debug.Log(String.Format(
"Capacity = {0}, Length = {1}, Position = {2}\n",
memStream.Capacity.ToString(),
memStream.Length.ToString(),
memStream.Position.ToString()));
// Set the position to the beginning of the stream.
memStream.Seek(0, SeekOrigin.Begin);
// Read the first 20 bytes from the stream.
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, 20);
// Read the remaining bytes, byte by byte.
while (count < memStream.Length)
{
byteArray[count++] =
Convert.ToByte(memStream.ReadByte());
}
// Decode the byte array into a char array
// and write it to the console.
charArray = new char[uniEncoding.GetCharCount(
byteArray, 0, count)];
uniEncoding.GetDecoder().GetChars(
byteArray, 0, count, charArray, 0);
Debug.Log(charArray);
}
}
示例3: PosTest3
public void PosTest3()
{
Char[] chars = GetCharArray(10);
Byte[] bytes = new Byte[20];
UnicodeEncoding uEncoding = new UnicodeEncoding();
int actualValue;
actualValue = uEncoding.GetBytes(chars, 0, 0, bytes, 20);
Assert.Equal(0, actualValue);
}
示例4: PosTest3
public void PosTest3()
{
String chars = GetString(10);
Byte[] bytes = new Byte[30];
UnicodeEncoding uEncoding = new UnicodeEncoding();
int actualValue;
actualValue = uEncoding.GetBytes(chars, 0, 0, bytes, 30);
Assert.Equal(0, actualValue);
}
示例5: GetAllData
public List<string> GetAllData()
{
string line = "";
string input_file = "./Saved Data/characters.xml";
List<string> keyList = new List<string>();
List<string> elemList = new List<string>();
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] key = null;
RijndaelManaged RMCrypto = new RijndaelManaged();
string tagID;
string tagIDend;
int indexStart = 0;
int indexEnd = 0;
key = encoding.GetBytes(Data_Handler_Key.keyvalue);
FileStream decrypted_file = new FileStream(input_file, FileMode.Open);
CryptoStream cryptography_stream = new CryptoStream(decrypted_file, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read);
using (MemoryStream msDecrypt = new MemoryStream())
{
using (BufferedStream readBuffer = new BufferedStream(cryptography_stream))
using (StreamReader srDecrypt = new StreamReader(readBuffer))
{
while ((line = srDecrypt.ReadLine()) != null)
{
elemList.Add(line);
}
}
}
cryptography_stream.Close();
decrypted_file.Close();
tagID = "<id>" + Character_Info.id + "</id>";
tagIDend = "<idend>" + Character_Info.id + "</idend>";
int i = 0;
foreach(var content in elemList)
{
if(content == tagID)
{
indexStart = i;
}
if(content == tagIDend)
{
indexEnd = i;
}
i++;
}
if (indexStart != indexEnd)
{
elemList.RemoveRange(indexStart, indexEnd - indexStart);
}
elemList.Remove("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
elemList.Remove("<savedcharacters>");
elemList.Remove("</savedcharacters>");
return elemList;
}
示例6: PosTest3
public void PosTest3()
{
Char[] srcChars = GetCharArray(10);
Char[] desChars = new Char[10];
Byte[] bytes = new Byte[20];
UnicodeEncoding uEncoding = new UnicodeEncoding();
int byteCount = uEncoding.GetBytes(srcChars, 0, 10, bytes, 0);
int actualValue;
actualValue = uEncoding.GetChars(bytes, 0, 0, desChars, 10);
Assert.Equal(0, actualValue);
}
示例7: NegTest1
public void NegTest1()
{
String chars = null;
Byte[] bytes = new Byte[30];
UnicodeEncoding uEncoding = new UnicodeEncoding();
int actualValue;
Assert.Throws<ArgumentNullException>(() =>
{
actualValue = uEncoding.GetBytes(chars, 0, 0, bytes, 0);
});
}
示例8: ToByteArray
public byte[] ToByteArray()
{
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] playerBytes = encoding.GetBytes(Player);
byte[] descBytes = encoding.GetBytes(Description);
byte[] bytes = new byte[12 + playerBytes.Length + descBytes.Length + Image.Length];
int arIndex = 0;
KLFCommon.IntToBytes(Index).CopyTo(bytes, arIndex);
arIndex += 4;
KLFCommon.IntToBytes(playerBytes.Length).CopyTo(bytes, arIndex);
arIndex += 4;
playerBytes.CopyTo(bytes, arIndex);
arIndex += playerBytes.Length;
KLFCommon.IntToBytes(descBytes.Length).CopyTo(bytes, arIndex);
arIndex += 4;
descBytes.CopyTo(bytes, arIndex);
arIndex += descBytes.Length;
Image.CopyTo(bytes, arIndex);
return bytes;
}
示例9: NegTest2
public void NegTest2()
{
Char[] chars = GetCharArray(10);
Byte[] bytes = null;
UnicodeEncoding uEncoding = new UnicodeEncoding();
int actualValue;
Assert.Throws<ArgumentNullException>(() =>
{
actualValue = uEncoding.GetBytes(chars, 0, 0, bytes, 0);
});
}
示例10: GenerateHash
// From http://www.a1vbcode.com/vbtip-149.asp
private String GenerateHash(String SourceText)
{
// Create an encoding object to ensure the encoding standard for the source text
var Ue = new UnicodeEncoding();
//Retrieve a byte array based on the source text
byte[] ByteSourceText = Ue.GetBytes(SourceText);
//Instantiate an MD5 Provider object
var Md5 = new MD5CryptoServiceProvider();
//Compute the hash value from the source
var ByteHash = Md5.ComputeHash(ByteSourceText);
//And convert it to String format for return
return Convert.ToBase64String(ByteHash);
}
示例11: PosTest2
public void PosTest2()
{
int expectedValue = _generator.GetInt16(-55) % 10 + 1;
int actualValue;
Char[] srcChars = GetCharArray(10);
Char[] desChars = new Char[10];
Byte[] bytes = new Byte[20];
UnicodeEncoding uEncoding = new UnicodeEncoding();
int byteCount = uEncoding.GetBytes(srcChars, 0, expectedValue, bytes, 0);
actualValue = uEncoding.GetChars(bytes, 0, expectedValue * 2, desChars, 0);
Assert.Equal(expectedValue, actualValue);
}
示例12: NegTest1
public void NegTest1()
{
Char[] srcChars = GetCharArray(10);
Char[] desChars = null;
Byte[] bytes = new Byte[20];
UnicodeEncoding uEncoding = new UnicodeEncoding();
int byteCount = uEncoding.GetBytes(srcChars, 0, 10, bytes, 0);
int actualValue;
Assert.Throws<ArgumentNullException>(() =>
{
actualValue = uEncoding.GetChars(bytes, 0, 0, desChars, 0);
});
}
示例13: PosTest4
public void PosTest4()
{
Char[] srcChars = GetCharArray(10);
Char[] desChars = new Char[10];
Byte[] bytes = new Byte[20];
UnicodeEncoding uEncoding = new UnicodeEncoding();
int byteCount = uEncoding.GetBytes(srcChars, 0, 10, bytes, 0);
String expectedValue = "";
String actualValue;
actualValue = uEncoding.GetString(bytes, 20, 0);
Assert.Equal(expectedValue, actualValue);
}
示例14: DeleteFile
public void DeleteFile()
{
string output_file = "characters.xml";
string character_directory = "Saved Data";
List<string> keyList = new List<string>();
List<string> contentList = new List<string>();
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] key = null;
RijndaelManaged RMCrypto = new RijndaelManaged();
string currentPath = Directory.GetCurrentDirectory();
Data_Loader Load = ScriptableObject.CreateInstance<Data_Loader>();
Data_Saver Save = ScriptableObject.CreateInstance<Data_Saver>();
List<string> tempList = new List<string>();
contentList = Save.GetAllData();
character_directory = Path.Combine(currentPath, character_directory);
output_file = Path.Combine(character_directory, output_file);
//Get key in byte form
XML_Loader XML = ScriptableObject.CreateInstance<XML_Loader>();
key = encoding.GetBytes(Data_Handler_Key.keyvalue);
//Collect data to be saved and write it to an encrypted xml file using the key retrieved earlier
FileStream encrypted_file = new FileStream(output_file, FileMode.Create);
CryptoStream cryptography_stream = new CryptoStream(encrypted_file, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (StreamWriter swEncrypt = new StreamWriter(cryptography_stream))
{
swEncrypt.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
swEncrypt.WriteLine("<savedcharacters>");
foreach (var content in contentList)
{
swEncrypt.WriteLine(content);
}
contentList.Clear();
swEncrypt.WriteLine("</savedcharacters>");
}
}
cryptography_stream.Close();
encrypted_file.Close();
tempList = Load.LoadCharacterIDs();
}
示例15: PosTest1
public bool PosTest1()
{
bool retVal = true;
Char[] srcChars = GetCharArray(10);
Char[] desChars = new Char[10];
Byte[] bytes = new Byte[20];
int buffer;
int outChars;
bool completed;
UnicodeEncoding uEncoding = new UnicodeEncoding();
int byteCount = uEncoding.GetBytes(srcChars, 0, 10, bytes, 0);
bool expectedValue = true;
bool actualValue = true;
TestLibrary.TestFramework.BeginScenario("PosTest1:Invoke the method");
try
{
Decoder dC = uEncoding.GetDecoder();
dC.Convert(bytes, 0, 20, desChars, 0, 10, true,out buffer,out outChars, out completed);
if (completed)
{
for (int i = 0; i < 10; i++)
{
actualValue = actualValue & (desChars[i] == srcChars[i]);
}
}
if (expectedValue != actualValue)
{
TestLibrary.TestFramework.LogError("001", "ExpectedValue(" + expectedValue + ") !=ActualValue(" + actualValue + ")" + " when chars is :" + ToString(srcChars));
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("002", "Unexpected exception:" + e + " when chars is :" + ToString(srcChars));
retVal = false;
}
return retVal;
}