Converting string to byte[] creates zero character

后端 未结 5 2746
死守一世寂寞
死守一世寂寞 2021-02-20 16:09

In this convert function

public static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCh         


        
5条回答
  •  半阙折子戏
    2021-02-20 16:49

    (97,0) is Unicode representation of 'a'. Unicode represents each character in two bytes. So you can not remove zeros. But you can change Encoding to ASCII. Try following for Converting string to byte[].

    byte[] array = Encoding.ASCII.GetBytes(input);
    

提交回复
热议问题