A weird thing in c# Encoding

后端 未结 6 1785
北海茫月
北海茫月 2020-12-11 21:52

I convert a byte array to a string , and I convert this string to byte array. these two byte arrays are different.

6条回答
  •  囚心锁ツ
    2020-12-11 22:37

    ASCII is 7-bit only, so others are invalid. By default it uses ? to replace any invalid bytes and that's why you get a ?.

    For 8-bit character sets, you should be looking for either the Extended ASCII (which is later defined "ISO 8859-1") or the code page 437 (which is often confused with Extended ASCII, but in fact it's not).

    You can use the following code:

    Encoding enc = Encoding.GetEncoding("iso-8859-1");
    // For CP437, use Encoding.GetEncoding(437)
    byte[] tmp = enc.GetBytes(enc.GetString(b));
    

提交回复
热议问题