# Encoding text for codepages

**URL:** <https://forums.thehomestack.io/t/encoding-text-for-codepages/292>\
**Category:** vb.Net\
**Created:** [January 5, 2008, 12:41am UTC](https://forums.thehomestack.io/t/encoding-text-for-codepages/292 "2008-01-05T00:41:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://forums.thehomestack.io/uploads/default/original/1X/161426793bc0d8bdf0d1a6692b3e44446861cc72.jpeg) [@system](https://forums.thehomestack.io/u/system)\
**Post date:** [January 5, 2008, 12:41am UTC](https://forums.thehomestack.io/t/encoding-text-for-codepages/292/1 "2008-01-05T00:41:19Z")

</div>

Encoding.Default, it will return the current ANSI codepage of  
the system(according to the machine’s System Locale) setting.

#Encoding.Default Property  
[http://msdn2.microsoft.com/en-us/library/system.text.encoding.default.aspx](http://msdn2.microsoft.com/en-us/library/system.text.encoding.default.aspx)

And from the disassemble code of the Encoding.Default, it use the following  
code logic:

===========

```auto
private static Encoding CreateDefaultEncoding()
{
      int num1 = Win32Native.GetACP();
      if (num1 == 0x4e4)
      {
            return new SBCSCodePageEncoding(num1);
      }
      return Encoding.GetEncoding(num1);
}

```

===========

so it is apparently that it is use the “GetACP”(get ANSI code page) windows  
platform API to query the current system ANSI code page.

Also, the “0x4e4” means 1252(decimal) which mapped to the windows  
1252(LATIN-1) charset. And this is the charset(Single Byte charset) which  
contains the sufficient characters for english(United States) and most  
west European countries. And on your system since the system locale is  
set to "English(United States) , the current ACP is “0x4e4”, so it  
internally return the SBCSCodePageEncoding(an internal class). For other  
system locale, such as Chinese(P.R.C), it will return other codepage  
number.

I can not tell you what’s the exact internally algorithm. The supported  
means to get the correct ANSI code page to use is to first determine the  
language-Region(locale) info. Then, use the windows API(or .net framework  
class) to query Codepage info from the locale info(language-Region).

In Win32 api, we use “GetLocaleInfo” method(with passing the  
LOCALE\_IDEFAULTANSICODEPAGE flag to query the default ANSI code page of  
that locale):

#GetLocaleInfo  
[http://msdn.microsoft.com/library/en-us/intl/nls\_34rz.asp?frame=true](http://msdn.microsoft.com/library/en-us/intl/nls_34rz.asp?frame=true)

In .net framework, we can use the CultureInfo class to construct a locale  
object and then use its “TextInfo” member to get the default ANSI code page  
(or any other text formatting or encoding information) of that locale. For  
example, the following code query the default ANSI code page of  
english(United states) and chinese(P.R.C) locale:

========================

```auto
CultureInfo ci1 = new CultureInfo("en-US");
        Encoding enc1 = Encoding.GetEncoding(ci1.TextInfo.ANSICodePage);
     Console.WriteLine(enc1.EncodingName);

CultureInfo ci2 = new CultureInfo("zh-CN");
        Encoding enc2 = Encoding.GetEncoding(ci2.TextInfo.ANSICodePage);

   Console.WriteLine(enc2.EncodingName);

```

===========================
