In this convert function
public static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCh
Try to specify Encoding
explicitly. You can use next code to convert string to bytes with specified encoding
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("abc");
if you print contents of bytes, you will get { 97,
98,
99 }
which doesn't contain zeros, as in your example
In your example default encoding using 16 bits per symbol. It can be observer by printing the results of
System.Text.Encoding.Unicode.GetBytes("abc"); // { 97, 0, 98, 0, 99, 0 }
Then while converting it back, you should select the appropriate encoding:
string str = System.Text.Encoding.ASCII.GetString(bytes);
Console.WriteLine (str);
Prints "abc"
as you might expected