CLR中的Encoding是在System.Text命名空间下的,它是一个抽象类(abstract class), 所以不能被直接实例化,它主要有如下的派生类:ASCIIEnding,UnicodeEncoding,UTF32Encoding,UTF7Encoding,UTF8Encoding,你可以根据需要选择一个合适的Encoding来进行编码和解码。你也可以调用Encoding的静态属性ASCII,Unicode,UTF32,UTF7,UTF8,来构造一个Encoding。其中Unicode是表示16位Encoding。调用静态属性和实例化一个子类的效果是一样的,如下代码。
1 Encoding encodingUTF8 = Encoding.UTF8;
2 Encoding encodingUTF8 = new UTF8Encoding(true);
以下是这些类型的一些简单描述:
ASCII编码 将16位字符编码成ASCII码,只能转换值小于Ox0080的16字符,并且被转换成单字节,就是说一个字符对应一个字节。当字符都在ASCII范围(0X00~0X7F)内时,可以用这种编码,它的速度非常快,适合于英美地区的字符。这种编码非常有限,汉字会被转换成乱码。在CLR对应ASCIIEndoing。
UTF-16 每个字符编码成2个字节,它不会对字符产生任何影响,也不会涉及到压缩处理,性能非常好,因为CLR中的字符也是16位的Unicode。在CLR中对应UnicodeEncoding。
UTF-32 使用4个字节编码成一个字符。从内存角度上讲,它并不是一种高效能的编码方案,因为第个字符都是4个字节,特别占内存,所以很少用来做文件和网络流的编码解码。在CLR中对应UTF32Encoding。
UTF-8 值在Ox0080之下的字符压缩成一个字符,也就是ASCII码;值在0X0080—0X07FF之间的字符都转换成2个字符,适合用于欧洲和中东地区。0X0800以上被转换成3个字符,适合于东亚地区的字符。代理项被转换成4个字节。因此,它是一种非常流行的编码,适用于互联网。它在处理0X0800以上的字符效率不好UTF-16。在CLR中对应UTF8Encoding。
UTF-7 这咱编码通常用于旧的系统,那时的系统是用7位值表示。目前已经被Unicode协淘汰。在CLR中对应UTF7Encoding。
从性能角度上来讲,如果你的代码需要在多处调用一个Encoding,微软建议你使用静态成员的方式构造一个Encoding对象,而不是构造实例。它的内部实现是一个单例模式。
public static Encoding UTF8
{
get
{
if (utf8Encoding == null)
{
utf8Encoding = new UTF8Encoding(true);
}
return utf8Encoding;
}
}
如果你知道某种编码的代码页(code page)或名字,那么你可以调用Encoding的静态方法GetEncoding(int codepage),GetEncoding(string name)来构造一个Encoding,比如我们常用的用于显示简体中文的gb2312,它的代码页是936,我们就可以这样定义:
Encoding encodingGB2312=Encoding.GetEncoding(“gb2312”);
Encoding encodingGB2312=Encoding.GetEncoding(936);
目前有几十种文字代码页,分别对应于不同的国家,不同的语言,它们只是对应Unicode字符集里的相一部分,比如说936,它只是对应于Unicode字符集里简体中文的那一部分,如果你想正确的显示繁体字,那么就要用中文繁体对应的代码页950。具体的代码页参照如下:
Info.CodePage Info.Name Info.DisplayName
代码页 名称 显示名
37 IBM037 IBM EBCDIC (US-Canada)
437 IBM437 OEM United States
500 IBM500 IBM EBCDIC (International)
708 ASMO-708 Arabic (ASMO 708)
720 DOS-720 Arabic (DOS)
737 ibm737 Greek (DOS)
775 ibm775 Baltic (DOS)
850 ibm850 Western European (DOS)
852 ibm852 Central European (DOS)
855 IBM855 OEM Cyrillic
857 ibm857 Turkish (DOS)
858 IBM00858 OEM Multilingual Latin I
860 IBM860 Portuguese (DOS)
861 ibm861 Icelandic (DOS)
862 DOS-862 Hebrew (DOS)
863 IBM863 French Canadian (DOS)
864 IBM864 Arabic (864)
865 IBM865 Nordic (DOS)
866 cp866 Cyrillic (DOS)
869 ibm869 Greek, Modern (DOS)
870 IBM870 IBM EBCDIC (Multilingual Latin-2)
874 windows-874 Thai (Windows)
875 cp875 IBM EBCDIC (Greek Modern)
932 shift_jis Japanese (Shift-JIS)
936 gb2312 Chinese Simplified (GB2312)
949 ks_c_5601-1987 Korean
950 big5 Chinese Traditional (Big5)
1026 IBM1026 IBM EBCDIC (Turkish Latin-5)
1047 IBM01047 IBM Latin-1
1140 IBM01140 IBM EBCDIC (US-Canada-Euro)
1141 IBM01141 IBM EBCDIC (Germany-Euro)
1142 IBM01142 IBM EBCDIC (Denmark-Norway-Euro)
1143 IBM01143 IBM EBCDIC (Finland-Sweden-Euro)
1144 IBM01144 IBM EBCDIC (Italy-Euro)
1145 IBM01145 IBM EBCDIC (Spain-Euro)
1146 IBM01146 IBM EBCDIC (UK-Euro)
1147 IBM01147 IBM EBCDIC (France-Euro)
1148 IBM01148 IBM EBCDIC (International-Euro)
1149 IBM01149 IBM EBCDIC (Icelandic-Euro)
1200 utf-16 Unicode
1201 unicodeFFFE Unicode (Big-Endian)
1250 windows-1250 Central European (Windows)
1251 windows-1251 Cyrillic (Windows)
1252 Windows-1252 Western European (Windows)
1253 windows-1253 Greek (Windows)
1254 windows-1254 Turkish (Windows)
1255 windows-1255 Hebrew (Windows)
1256 windows-1256 Arabic (Windows)
1257 windows-1257 Baltic (Windows)
1258 windows-1258 Vietnamese (Windows)
1361 Johab Korean (Johab)
10000 macintosh Western European (Mac)
10001 x-mac-japanese Japanese (Mac)
10002 x-mac-chinesetrad Chinese Traditional (Mac)
10003 x-mac-korean Korean (Mac)
10004 x-mac-arabic Arabic (Mac)
10005 x-mac-hebrew Hebrew (Mac)
10006 x-mac-greek Greek (Mac)
10007 x-mac-cyrillic Cyrillic (Mac)
10008 x-mac-chinesesimp Chinese Simplified (Mac)
10010 x-mac-romanian Romanian (Mac)
10017 x-mac-ukrainian Ukrainian (Mac)
10021 x-mac-thai Thai (Mac)
10029 x-mac-ce Central European (Mac)
10079 x-mac-icelandic Icelandic (Mac)
10081 x-mac-turkish Turkish (Mac)
10082 x-mac-croatian Croatian (Mac)
20000 x-Chinese-CNS Chinese Traditional (CNS)
20001 x-cp20001 TCA Taiwan
20002 x-Chinese-Eten Chinese Traditional (Eten)
20003 x-cp20003 IBM5550 Taiwan
20004 x-cp20004 TeleText Taiwan
20005 x-cp20005 Wang Taiwan
20105 x-IA5 Western European (IA5)
20106 x-IA5-German German (IA5)
20107 x-IA5-Swedish Swedish (IA5)
20108 x-IA5-Norwegian Norwegian (IA5)
20127 us-ascii US-ASCII
20261 x-cp20261 T.61
20269 x-cp20269 ISO-6937
20273 IBM273 IBM EBCDIC (Germany)
20277 IBM277 IBM EBCDIC (Denmark-Norway)
20278 IBM278 IBM EBCDIC (Finland-Sweden)
20280 IBM280 IBM EBCDIC (Italy)
20284 IBM284 IBM EBCDIC (Spain)
20285 IBM285 IBM EBCDIC (UK)
20290 IBM290 IBM EBCDIC (Japanese katakana)
20297 IBM297 IBM EBCDIC (France)
20420 IBM420 IBM EBCDIC (Arabic)
20423 IBM423 IBM EBCDIC (Greek)
20424 IBM424 IBM EBCDIC (Hebrew)
20833 x-EBCDIC-KoreanExtended IBM EBCDIC (Korean Extended)
20838 IBM-Thai IBM EBCDIC (Thai)
20866 koi8-r Cyrillic (KOI8-R)
20871 IBM871 IBM EBCDIC (Icelandic)
20880 IBM880 IBM EBCDIC (Cyrillic Russian)
20905 IBM905 IBM EBCDIC (Turkish)
20924 IBM00924 IBM Latin-1
20932 EUC-JP Japanese (JIS 0208-1990 and 0212-1990)
20936 x-cp20936 Chinese Simplified (GB2312-80)
20949 x-cp20949 Korean Wansung
21025 cp1025 IBM EBCDIC (Cyrillic Serbian-Bulgarian)
21866 koi8-u Cyrillic (KOI8-U)
28591 iso-8859-1 Western European (ISO)
28592 iso-8859-2 Central European (ISO)
28593 iso-8859-3 Latin 3 (ISO)
28594 iso-8859-4 Baltic (ISO)
28595 iso-8859-5 Cyrillic (ISO)
28596 iso-8859-6 Arabic (ISO)
28597 iso-8859-7 Greek (ISO)
28598 iso-8859-8 Hebrew (ISO-Visual)
28599 iso-8859-9 Turkish (ISO)
28603 iso-8859-13 Estonian (ISO)
28605 iso-8859-15 Latin 9 (ISO)
29001 x-Europa Europa
38598 iso-8859-8-i Hebrew (ISO-Logical)
50220 iso-2022-jp Japanese (JIS)
50221 csISO2022JP Japanese (JIS-Allow 1 byte Kana)
50222 iso-2022-jp Japanese (JIS-Allow 1 byte Kana - SO/SI)
50225 iso-2022-kr Korean (ISO)
50227 x-cp50227 Chinese Simplified (ISO-2022)
51932 euc-jp Japanese (EUC)
51936 EUC-CN Chinese Simplified (EUC)
51949 euc-kr Korean (EUC)
52936 hz-gb-2312 Chinese Simplified (HZ)
54936 GB18030 Chinese Simplified (GB18030)
57002 x-iscii-de ISCII Devanagari
57003 x-iscii-be ISCII Bengali
57004 x-iscii-ta ISCII Tamil
57005 x-iscii-te ISCII Telugu
57006 x-iscii-as ISCII Assamese
57007 x-iscii-or ISCII Oriya
57008 x-iscii-ka ISCII Kannada
57009 x-iscii-ma ISCII Malayalam
57010 x-iscii-gu ISCII Gujarati
57011 x-iscii-pa ISCII Punjabi
65000 utf-7 Unicode (UTF-7)
65001 utf-8 Unicode (UTF-8)
65005 utf-32 Unicode (UTF-32)
65006 utf-32BE Unicode (UTF-32 Big-Endian)