Convert ANSI to char in .NET

Sometimes you have a byte array that is an ANSI string. To convert it to a char array or a string in C#, you need to apply a conversion. It appears that many folks are using:

char val = Encoding.ASCII.GetChars(byteArray);

This is plain and simple wrong. The ASCII encoder is a 7-bit encoder that can only be used for ASCII data.
Unless you are interfacing with a CP/M machine or a 9-dot IBM matrix printer, you are unlikely dealing with ASCII.
Most likely you are talking about ANSI, or more precisely, about a Windows code page such as 1252 for Western
countries, 1251 for many East European countries, and so on. Hence, if you created some so called "ASCII" data on
a European or US Windows computer, what you actually want to use is:

char val = Encoding.GetEncoding(1252).GetChars(byteValue);



char val = Encoding.Default.GetChars(byteValue);