Encoding 类
表示字符编码。
**命名空间:**System.Text
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public MustInherit Class Encoding
Implements ICloneable
用法
Dim instance As Encoding
[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class Encoding : ICloneable
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class Encoding abstract : ICloneable
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public abstract class Encoding implements ICloneable
SerializableAttribute
ComVisibleAttribute(true)
public abstract class Encoding implements ICloneable
备注
编码是一个将一组 Unicode 字符转换为一个字节序列的过程。解码是一个反向操作过程,即将一个编码字节序列转换为一组 Unicode 字符。
Unicode 标准为所有支持脚本中的每个字符分配一个码位(一个数字)。Unicode 转换格式 (UTF) 是一种码位编码方式。Unicode 标准 3.2 版使用下列 UTF:
UTF-8,它将每个码位表示为一个由 1 至 4 个字节组成的序列。
UTF-16,它将每个码位表示为一个由 1 至 2 个 16 位整数组成的序列。
UTF-32,它将每个码位表示为一个 32 位整数。
.NET Framework 提供以下 Encoding 类的实现以支持当前 Unicode 编码和其他编码:
ASCIIEncoding 将 Unicode 字符编码为单个 7 位 ASCII 字符。此编码仅支持 U+0000 和 U+007F 之间的字符值。代码页 20127。还可通过 ASCII 属性获得。
UTF7Encoding 使用 UTF-7 编码对 Unicode 字符进行编码。此编码支持所有 Unicode 字符值。代码页 65000。还可通过 UTF7 属性获得。
UTF8Encoding 使用 UTF-8 编码对 Unicode 字符进行编码。此编码支持所有 Unicode 字符值。代码页 65001。还可通过 UTF8 属性获得。
UnicodeEncoding 使用 UTF-16 编码对 Unicode 字符进行编码。支持 Little-Endian(代码页 1200)和 Big-Endian(代码页 1201)字节顺序。还可通过 Unicode 属性和 BigEndianUnicode 属性获得。
UTF32Encoding 使用 UTF-32 编码对 Unicode 字符进行编码。支持 Little-Endian(代码页 65005)和 Big-Endian(代码页 65006)字节顺序。还可通过 UTF32 属性获得。
使用 GetEncoding 方法以获取其他编码。使用 GetEncodings 方法来获取所有编码的列表。
下表列出编码以及与编码关联的代码页。最后一列中的星号指示 .NET Framework 本身即支持该代码页,而不需考虑基础平台。
代码页 |
名称 |
显示名称 |
|
---|---|---|---|
37 |
IBM037 |
IBM EBCDIC(美国 - 加拿大) |
|
437 |
IBM437 |
OEM 美国 |
|
500 |
IBM500 |
IBM EBCDIC(国际) |
|
708 |
ASMO-708 |
阿拉伯字符 (ASMO 708) |
|
720 |
DOS-720 |
阿拉伯字符 (DOS) |
|
737 |
ibm737 |
希腊字符 (DOS) |
|
775 |
ibm775 |
波罗的海字符 (DOS) |
|
850 |
ibm850 |
西欧字符 (DOS) |
|
852 |
ibm852 |
中欧字符 (DOS) |
|
855 |
IBM855 |
OEM 西里尔语 |
|
857 |
ibm857 |
土耳其字符 (DOS) |
|
858 |
IBM00858 |
OEM 多语言拉丁语 I |
|
860 |
IBM860 |
葡萄牙语 (DOS) |
|
861 |
ibm861 |
冰岛语 (DOS) |
|
862 |
DOS-862 |
希伯来字符 (DOS) |
|
863 |
IBM863 |
加拿大法语 (DOS) |
|
864 |
IBM864 |
阿拉伯字符 (864) |
|
865 |
IBM865 |
北欧字符 (DOS) |
|
866 |
cp866 |
西里尔字符 (DOS) |
|
869 |
ibm869 |
现代希腊字符 (DOS) |
|
870 |
IBM870 |
IBM EBCDIC(多语言拉丁语 2) |
|
874 |
windows-874 |
泰语 (Windows) |
|
875 |
cp875 |
IBM EBCDIC(现代希腊语) |
|
932 |
shift_jis |
日语 (Shift-JIS) |
|
936 |
gb2312 |
简体中文 (GB2312) |
* |
949 |
ks_c_5601-1987 |
朝鲜语 |
|
950 |
big5 |
繁体中文 (Big5) |
|
1026 |
IBM1026 |
IBM EBCDIC(土耳其拉丁语 5) |
|
1047 |
IBM01047 |
IBM 拉丁语 1 |
|
1140 |
IBM01140 |
IBM EBCDIC(美国 - 加拿大 - 欧洲) |
|
1141 |
IBM01141 |
IBM EBCDIC(德国 - 欧洲) |
|
1142 |
IBM01142 |
IBM EBCDIC(丹麦 - 挪威 - 欧洲) |
|
1143 |
IBM01143 |
IBM EBCDIC(芬兰 - 瑞典 - 欧洲) |
|
1144 |
IBM01144 |
IBM EBCDIC(意大利 - 欧洲) |
|
1145 |
IBM01145 |
IBM EBCDIC(西班牙 - 欧洲) |
|
1146 |
IBM01146 |
IBM EBCDIC(英国 - 欧洲) |
|
1147 |
IBM01147 |
IBM EBCDIC(法国 - 欧洲) |
|
1148 |
IBM01148 |
IBM EBCDIC(国际 - 欧洲) |
|
1149 |
IBM01149 |
IBM EBCDIC(冰岛语 - 欧洲) |
|
1200 |
utf-16 |
Unicode |
* |
1201 |
UnicodeFFFE |
Unicode (Big-Endian) |
* |
1250 |
windows-1250 |
中欧字符 (Windows) |
|
1251 |
windows-1251 |
西里尔字符 (Windows) |
|
1252 |
Windows-1252 |
西欧字符 (Windows) |
* |
1253 |
windows-1253 |
希腊字符 (Windows) |
|
1254 |
windows-1254 |
土耳其字符 (Windows) |
|
1255 |
windows-1255 |
希伯来字符 (Windows) |
|
1256 |
windows-1256 |
阿拉伯字符 (Windows) |
|
1257 |
windows-1257 |
波罗的海字符 (Windows) |
|
1258 |
windows-1258 |
越南字符 (Windows) |
|
1361 |
Johab |
朝鲜语 (Johab) |
|
10000 |
macintosh |
西欧字符 (Mac) |
|
10001 |
x-mac-japanese |
日语 (Mac) |
|
10002 |
x-mac-chinesetrad |
繁体中文 (Mac) |
|
10003 |
x-mac-korean |
朝鲜语 (Mac) |
* |
10004 |
x-mac-arabic |
阿拉伯字符 (Mac) |
|
10005 |
x-mac-hebrew |
希伯来字符 (Mac) |
|
10006 |
x-mac-greek |
希腊字符 (Mac) |
|
10007 |
x-mac-cyrillic |
西里尔字符 (Mac) |
|
10008 |
x-mac-chinesesimp |
简体中文 (Mac) |
* |
10010 |
x-mac-romanian |
罗马尼亚语 (Mac) |
|
10017 |
x-mac-ukrainian |
乌克兰语 (Mac) |
|
10021 |
x-mac-thai |
泰语 (Mac) |
|
10029 |
x-mac-ce |
中欧字符 (Mac) |
|
10079 |
x-mac-icelandic |
冰岛语 (Mac) |
|
10081 |
x-mac-turkish |
土耳其字符 (Mac) |
|
10082 |
x-mac-croatian |
克罗地亚语 (Mac) |
|
20000 |
x-Chinese-CNS |
繁体中文 (CNS) |
|
20001 |
x-cp20001 |
TCA 台湾 |
|
20002 |
x-Chinese-Eten |
繁体中文 (Eten) |
|
20003 |
x-cp20003 |
IBM5550 台湾 |
|
20004 |
x-cp20004 |
TeleText 台湾 |
|
20005 |
x-cp20005 |
Wang 台湾 |
|
20105 |
x-IA5 |
西欧字符 (IA5) |
|
20106 |
x-IA5-German |
德语 (IA5) |
|
20107 |
x-IA5-Swedish |
瑞典语 (IA5) |
|
20108 |
x-IA5-Norwegian |
挪威语 (IA5) |
|
20127 |
us-ascii |
US-ASCII |
* |
20261 |
x-cp20261 |
T.61 |
|
20269 |
x-cp20269 |
ISO-6937 |
|
20273 |
IBM273 |
IBM EBCDIC(德国) |
|
20277 |
IBM277 |
IBM EBCDIC(丹麦 - 挪威) |
|
20278 |
IBM278 |
IBM EBCDIC(芬兰 - 瑞典) |
|
20280 |
IBM280 |
IBM EBCDIC(意大利) |
|
20284 |
IBM284 |
IBM EBCDIC(西班牙) |
|
20285 |
IBM285 |
IBM EBCDIC(英国) |
|
20290 |
IBM290 |
IBM EBCDIC(日语片假名) |
|
20297 |
IBM297 |
IBM EBCDIC(法国) |
|
20420 |
IBM420 |
IBM EBCDIC(阿拉伯语) |
|
20423 |
IBM423 |
IBM EBCDIC(希腊语) |
|
20424 |
IBM424 |
IBM EBCDIC(希伯来语) |
|
20833 |
x-EBCDIC-KoreanExtended |
IBM EBCDIC(朝鲜语扩展) |
|
20838 |
IBM-Thai |
IBM EBCDIC(泰语) |
|
20866 |
koi8-r |
西里尔字符 (KOI8-R) |
|
20871 |
IBM871 |
IBM EBCDIC(冰岛语) |
|
20880 |
IBM880 |
IBM EBCDIC(西里尔俄语) |
|
20905 |
IBM905 |
IBM EBCDIC(土耳其语) |
|
20924 |
IBM00924 |
IBM 拉丁语 1 |
|
20932 |
EUC-JP |
日语(JIS 0208-1990 和 0212-1990) |
|
20936 |
x-cp20936 |
简体中文 (GB2312-80) |
* |
20949 |
x-cp20949 |
朝鲜语 Wansung |
* |
21025 |
cp1025 |
IBM EBCDIC(西里尔塞尔维亚 - 保加利亚语) |
|
21866 |
koi8-u |
西里尔字符 (KOI8-U) |
|
28591 |
iso-8859-1 |
西欧字符 (ISO) |
* |
28592 |
iso-8859-2 |
中欧字符 (ISO) |
|
28593 |
iso-8859-3 |
拉丁语 3 (ISO) |
|
28594 |
iso-8859-4 |
波罗的海字符 (ISO) |
|
28595 |
iso-8859-5 |
西里尔字符 (ISO) |
|
28596 |
iso-8859-6 |
阿拉伯字符 (ISO) |
|
28597 |
iso-8859-7 |
希腊字符 (ISO) |
|
28598 |
iso-8859-8 |
希伯来字符 (ISO-Visual) |
* |
28599 |
iso-8859-9 |
土耳其字符 (ISO) |
|
28603 |
iso-8859-13 |
爱沙尼亚语 (ISO) |
|
28605 |
iso-8859-15 |
拉丁语 9 (ISO) |
|
29001 |
x-Europa |
欧罗巴 |
|
38598 |
iso-8859-8-i |
希伯来字符 (ISO-Logical) |
* |
50220 |
iso-2022-jp |
日语 (JIS) |
* |
50221 |
csISO2022JP |
日语(JIS- 允许 1 字节假名) |
* |
50222 |
iso-2022-jp |
日语(JIS- 允许 1 字节假名 - SO/SI) |
* |
50225 |
iso-2022-kr |
朝鲜语 (ISO) |
* |
50227 |
x-cp50227 |
简体中文 (ISO-2022) |
* |
51932 |
euc-jp |
日语 (EUC) |
* |
51936 |
EUC-CN |
简体中文 (EUC) |
* |
51949 |
euc-kr |
朝鲜语 (EUC) |
* |
52936 |
hz-gb-2312 |
简体中文 (HZ) |
* |
54936 |
GB18030 |
简体中文 (GB18030) |
* |
57002 |
x-iscii-de |
ISCII 梵文 |
* |
57003 |
x-iscii-be |
ISCII 孟加拉语 |
* |
57004 |
x-iscii-ta |
ISCII 泰米尔语 |
* |
57005 |
x-iscii-te |
ISCII 泰卢固语 |
* |
57006 |
x-iscii-as |
ISCII 阿萨姆语 |
* |
57007 |
x-iscii-or |
ISCII 奥里雅语 |
* |
57008 |
x-iscii-ka |
ISCII 卡纳达语 |
* |
57009 |
x-iscii-ma |
ISCII 马拉雅拉姆语 |
* |
57010 |
x-iscii-gu |
ISCII 古吉拉特语 |
* |
57011 |
x-iscii-pa |
ISCII 旁遮普语 |
* |
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) |
* |
GetByteCount 方法确定将有多少字节对一组 Unicode 字符进行编码,而 GetBytes 方法将执行实际的编码操作。
同样,GetCharCount 方法确定将有多少字符对字节序列进行解码,而 GetChars 方法执行实际的解码。
如果要转换的数据仅存在于连续块(如从流中读取的数据)中,或者数据量很大,需要划分为较小的块,则可使用由某个派生类的 GetDecoder 方法提供的 Decoder 或由派生类的 GetEncoder 方法提供的 Encoder。
UTF-16 和 UTF-32 编码器可以使用 Big-Endian 字节顺序(从最高有效字节开始),也可以使用 Little-Endian 字节顺序(从最低有效字节开始)。例如,大写拉丁字母 A (U+0041) 的序列化结果(十六进制)如下所示:
UTF-16 Big-Endian 字节顺序:00 41
UTF-16 Little-Endian 字节顺序:41 00
UTF-32 Big-Endian 字节顺序:00 00 00 41
UTF-32 Little-Endian 字节顺序:41 00 00 00
或者,Encoding 提供一个前导码(即一个字节数组),可以将它作为编码过程中所产生的字节序列的前缀。如果前导码中包含字节顺序标记(在 Unicode 中,码位为 U+FEFF),则它会帮助解码器确定字节顺序和转换格式或 UTF。Unicode 字节顺序标记的序列化结果(十六进制)如下所示:
UTF-8:EF BB BF
UTF-16 Big-Endian 字节顺序:FE FF
UTF-16 Little-Endian 字节顺序:FF FE
UTF-32 Big-Endian 字节顺序:00 00 FE FF
UTF-32 Little-Endian 字节顺序:FF FE 00 00
通常,使用本机字节顺序存储 Unicode 字符的效率更高。例如,在 Little-Endian 平台(如 Intel 计算机)上最好使用 Little-Endian 字节顺序。
GetPreamble 方法返回一个可以包括字节顺序标记的字节数组。如果将此字节数组作为编码流的前缀,将有助于解码器识别所用的编码格式。
有关字节顺序和字节顺序标记的更多信息,请参见 www.unicode.org 上的“The Unicode Standard”(Unicode 标准)部分。
示例
下面的代码示例将字符串从一种编码转换为另一种编码。
Imports System
Imports System.Text
Imports Microsoft.VisualBasic
Namespace Convert_Example
Class MyConvertExampleClass
Shared Sub Main()
Dim unicodeString As String = "This string contains the unicode character Pi(" & ChrW(&H03A0) & ")"
' Create two different encodings.
Dim ascii As Encoding = Encoding.ASCII
Dim [unicode] As Encoding = Encoding.Unicode
' Convert the string into a byte[].
Dim unicodeBytes As Byte() = [unicode].GetBytes(unicodeString)
' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert([unicode], ascii, unicodeBytes)
' Convert the new byte[] into a char[] and then into a string.
' This is a slightly different approach to converting to illustrate
' the use of GetCharCount/GetChars.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)) As Char
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)
' Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString)
Console.WriteLine("Ascii converted string: {0}", asciiString)
End Sub
End Class
End Namespace
using System;
using System.Text;
namespace ConvertExample
{
class ConvertExampleClass
{
static void Main()
{
string unicodeString = "This string contains the unicode character Pi(\u03a0)";
// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);
// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
}
}
}
using namespace System;
using namespace System::Text;
int main()
{
String^ unicodeString = "This string contains the unicode character Pi(\u03a0)";
// Create two different encodings.
Encoding^ ascii = Encoding::ASCII;
Encoding^ unicode = Encoding::Unicode;
// Convert the string into a Byte->Item[].
array<Byte>^unicodeBytes = unicode->GetBytes( unicodeString );
// Perform the conversion from one encoding to the other.
array<Byte>^asciiBytes = Encoding::Convert( unicode, ascii, unicodeBytes );
// Convert the new Byte into[] a char and[] then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
array<Char>^asciiChars = gcnew array<Char>(ascii->GetCharCount( asciiBytes, 0, asciiBytes->Length ));
ascii->GetChars( asciiBytes, 0, asciiBytes->Length, asciiChars, 0 );
String^ asciiString = gcnew String( asciiChars );
// Display the strings created before and after the conversion.
Console::WriteLine( "Original String*: {0}", unicodeString );
Console::WriteLine( "Ascii converted String*: {0}", asciiString );
}
package ConvertExample;
import System.*;
import System.Text.*;
class ConvertExampleClass
{
public static void main(String[] args)
{
String unicodeString =
"This string contains the unicode character Pi(\u03a0)";
// Create two different encodings.
Encoding ascii = Encoding.get_ASCII();
Encoding unicode = Encoding.get_Unicode();
// Convert the string into a byte[].
ubyte unicodeBytes[] = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
ubyte asciiBytes[] = Encoding.Convert(unicode, ascii, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char asciiChars[] = new
char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.length, asciiChars, 0);
String asciiString = new String(asciiChars);
// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
} //main
} //ConvertExampleClass
继承层次结构
System.Object
System.Text.Encoding
派生类
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0