本文介绍了C#和Encoding.ASCII.GetString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
byte[] header = new byte[]{255, 216};
string ascii = Encoding.ASCII.GetString(header);
我希望ASCII等于要FFD8(JPEG SOI标记)
I expect ASCII to be equal to be FFD8 (JPEG SOI marker)
相反,我得到????
推荐答案
在这种情况下,你最好来比较字节数组,而不是转换为字符串。
In this case you'd be better to compare the byte arrays rather than converting to string.
如果您必须转换为字符串,我建议使用编码的Latin-1又名ISO-8859-1又名code 28591页编码,这个编码将映射与十六进制值的所有字节的范围是0-255用相同的十六进制值的统一code字 - 方便了此方案。以下任何方式将得到这个编码方式:
If you must convert to string, I suggest using the encoding Latin-1 aka ISO-8859-1 aka Code Page 28591 encoding, as this encoding will map all bytes with hex values are in the range 0-255 to the Unicode character with the same hex value - convenient for this scenario. Any of the following will get this encoding:
Encoding.GetEncoding(28591)
Encoding.GetEncoding("Latin1")
Encoding.GetEncoding("ISO-8859-1")
这篇关于C#和Encoding.ASCII.GetString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!