TheDude
01-04-2008, 07:41 PM
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
And from the disassemble code of the Encoding.Default, it use the following
code logic:
===========
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
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:
========================
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);
===========================
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
And from the disassemble code of the Encoding.Default, it use the following
code logic:
===========
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
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:
========================
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);
===========================