ANSI Encoding in C#

By Kevin Walker

Microsoft Visual C# encodes all text, by default, using Unicode (UTF-8). This makes it easier to ensure that text appears correctly on systems all over the world without the issues that surround the use of ANSI code pages. However, access to ANSI is still available for those that need it.

ANSI Code Pages

ANSI works by encoding text according to the values listed in a code page. There are dozens (if not hundreds) of code pages. Each of them is designed to allow text to be represented according to the needs of a language or application.

ANSI Encoding in C#

The following code will store a string according to the default ANSI Windows Enligsh code page: String s = "A unicode string."; Encoding winLatinCodePage = Encoding.GetEncoding(1252); Byte[] bytes = Encoding.Convert(Encoding.UTF8, winLatinCodePage,Encoding.UTF8.GetBytes(s));A listing of all the supported code pages is available at the Microsoft Developers Network's page on Encodings.


ANSI was replaced in Visual C# for good reason: it translates poorly to other languages. Unless you know the code page number in advance, it is nearly impossible to correctly interpret the text. Many code pages are contradictory, particularly when it comes to how to store accented letters like the é in café. Unless you have a very good reason to use ANSI, it is best to stick to Unicode.