简体   繁体   English

从字节数组转换为字符串时的结果奇怪

[英]Strange results when converting from byte array to string

I get strange results when converting byte array to string and then converting the string back to byte array. 将字节数组转换为字符串,然后将字符串转换回字节数组时,得到奇怪的结果。

Try this: 尝试这个:

     byte[] b = new byte[1];
    b[0] = 172;
    string s = Encoding.ASCII.GetString(b);

    byte[] b2 = Encoding.ASCII.GetBytes(s);
    MessageBox.Show(b2[0].ToString());

And the result for me is not 172 as I'd expect but... 63. 结果对我来说不是172,而是63。

Why does it happen? 为什么会发生?

Why does it happen? 为什么会发生?

Because ASCII only contains values up to 127. 因为ASCII仅包含最多127个值。

When faced with binary data which is invalid for the given encoding, Encoding.GetString can provide a replacement character, or throw an exception. 当遇到对于给定编码无效的二进制数据时, Encoding.GetString可以提供替换字符或引发异常。 Here, it's using a replacement character of ? 在这里,它使用的替换字符是? .

It's not clear exactly what you're trying to achieve, but: 目前尚不清楚您要实现的目标,但是:

  • If you're converting arbitrary binary data to text, use Convert.ToBase64String instead; 如果要将任意二进制数据转换为文本,请改用Convert.ToBase64String ; do not try to use an encoding, as you're not really representing text. 不要尝试使用的编码,因为你没有真正代表文本。 You can use Convert.FromBase64String to then decode. 您可以使用Convert.FromBase64String进行解码。
  • Encoding.ASCII is usually a bad choice, and certainly binary data including a byte of 172 is not ASCII text Encoding.ASCII通常是一个错误的选择,当然包括172字节的二进制数据不是 ASCII文本
  • You need to work out which encoding you're actually using. 您需要确定您实际使用的编码。 Personally I dislike using Encoding.Default unless you really know the data is in the default encoding for the platform you're working on. 就我个人而言,我不喜欢使用Encoding.Default除非您真的知道数据是所使用平台的默认编码。 If you get the choice, using UTF-8 is a good one. 如果可以的话,使用UTF-8是一个不错的选择。

ASCII encoding is a 7-bit encoding. ASCII编码是7位编码。 If you take a look into generated string it contains "?" 如果查看生成的字符串,它包含“?” - unrecognized character. -无法识别的角色。 You might choose Encoding.Default instead. 您可以选择Encoding.Default。

ASCII is a seven bit character encoding, so 172 falls out of that range, so when converting to a string, it converts to "?" ASCII是七位字符编码,因此172不在该范围内,因此在转换为字符串时,它将转换为“?” which is used for characters that cannot be represented. 用于无法表示的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM