Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding.GetEncoding(437).GetString() bug?

I have following test program

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);

This produces following result:

c: 167
b: 21
c1: 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?

like image 623
SeeR Avatar asked Aug 08 '11 15:08

SeeR


1 Answers

CP437 is not "two-way" for characters in the range 0-31. As stated in the Wikipedia page you linked:

For many uses, the codes in the range 0 to 31 and the code 127 will not produce these symbols. Some (or all) of them will be interpreted as ASCII control characters.

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.

like image 155
Julien Lebosquain Avatar answered Oct 14 '22 09:10

Julien Lebosquain