Converting string to byte[] creates zero character

后端 未结 5 2843
死守一世寂寞
死守一世寂寞 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:36

    In reality .net (at least for 4.0) automatically changes size of char when serialized with BinaryWriter

    UTF-8 chars have variable length (might not be 1 byte), ASCII chars have 1 byte

    'ē' = 2 bytes

    'e' = 1 byte

    It must be kept in mind when using

    BinaryReader.ReadChars(stream)
    

    In case of word "ēvalds" = 7 bytes size will be different than "evalds" = 6 bytes

提交回复
热议问题