问题描述
我有以下测试程序
char c = '§';
Debug.WriteLine("c: " + (int)c);
byte b = Encoding.GetEncoding(437).GetBytes("§")[0];
Debug.WriteLine("b: " + b);
char c1 = Encoding.GetEncoding(437).GetString(new byte[] { 21 })[0];
Debug.WriteLine("c1: " + (int)c1);
这将产生以下结果:
c: 167
b: 21
c1: 21
我可以看到 GetBytes正常工作
167 unicode => CP437中的21
但GetString不起作用
CP437中的
21 => Unicode中的21
As I can see here GetBytes is working correctly
167 in unicode => 21 in CP437
but GetString is not working
21 in CP437 => 21 in unicode
这是是错误还是我的错误?
Is this a bug or my mistake?
推荐答案
CP437对于0-31范围内的字符不是双向的。如Wikipedia页面中所述,您已链接:
CP437 is not "two-way" for characters in the range 0-31. As stated in the Wikipedia page you linked:
将Unicode字符映射到受支持的CP437此范围内的字符有效,但反之则不行。例如,以字节13和10表示的字符为例:很有可能,如果将它们放入CP437字符串中,则实际上希望保留回车符和换行符,而不转换为项目符号和音符。这是正常现象:不是错误。
Mapping an Unicode character to a supported CP437 character that is in this range works, but not the other way around. For example, take characters represented by bytes 13 and 10: chances are that if you got them inside a CP437 string, you actually want carriage return and line feed characters to be preserved, and not converted to a bullet and a music note. This behavior is normal: it's not a bug.
这篇关于Encoding.GetEncoding(437).GetString()错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!