I found this little snippet to to transform a string into an array of bytes:
public byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
And this one to transform an array of bytes into a string:
public string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
But I notice that the first one returns an array twice as big as the initial string (because sizeof(char) = 2) and every other slot in my array is a 0.
Example:
string = TEST
bytes[] = { 84, 0, 69, 0, 83, 0, 84, 0 };
I'm using this function to send packets in UDP, so I need my packets to be the smallest possible.
Why is the array twice bigger? How do I fix it?
.NET actually uses UTF-16 encoding to store string's and char's, which means each character is actually encoded with 2 bytes. This is detailed in Character Encoding in the .NET Framework:
UTF-16 encoding is used by the common language runtime to represent
CharandStringvalues, and it is used by the Windows operating system to representWCHARvalues.
So you should expect to get 2 bytes for every character in your string.
If you want to only get 1 byte for per character you have to use a different encoding. For this input, ASCII encoding will work:
public byte[] GetBytes(string str)
{
return System.Text.Encoding.ASCII.GetBytes(str);
}
Calling this with the input "TEST" will return { 84, 69, 83, 84 }
To get bytes for a string use:
Encoding.Utf8.GetBytes()
http://msdn.microsoft.com/en-us/library/system.text.encoding.getbytes(v=vs.110).aspx
To go back to string use:
Encoding.Utf8.GetString()
http://msdn.microsoft.com/en-us/library/744y86tc(v=vs.110).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With