本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

Encoding 类

 

表示字符编码。

若要浏览此类型的 .NET Framework 源代码,请参阅引用源

命名空间:   System.Text
程序集:  mscorlib(位于 mscorlib.dll)


[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class Encoding : ICloneable

名称说明
System_CAPS_protmethodEncoding()

初始化 Encoding 类的新实例。

System_CAPS_protmethodEncoding(Int32)

初始化对应于指定代码页的 Encoding 类的新实例。

System_CAPS_protmethodEncoding(Int32, EncoderFallback, DecoderFallback)

使用指定编码器和解码器回退策略初始化对应于指定代码页的 Encoding 类的新实例。

名称说明
System_CAPS_pubpropertySystem_CAPS_staticASCII

获取 ASCII(7 位)字符集的编码。

System_CAPS_pubpropertySystem_CAPS_staticBigEndianUnicode

获取使用 Big Endian 字节顺序的 UTF-16 格式的编码。

System_CAPS_pubpropertyBodyName

在派生类中重写时,获取可与邮件代理正文标记一起使用的当前编码的名称。

System_CAPS_pubpropertyCodePage

在派生类中重写时,获取当前 Encoding 的代码页标识符。

System_CAPS_pubpropertyDecoderFallback

获取或设置当前 DecoderFallback 对象的 Encoding 对象。

System_CAPS_pubpropertySystem_CAPS_staticDefault

获取操作系统的当前 ANSI 代码页的编码。

System_CAPS_pubpropertyEncoderFallback

获取或设置当前 EncoderFallback 对象的 Encoding 对象。

System_CAPS_pubpropertyEncodingName

在派生类中重写时,获取当前编码的用户可读说明。

System_CAPS_pubpropertyHeaderName

在派生类中重写时,获取可与邮件代理头标记一起使用的当前编码的名称。

System_CAPS_pubpropertyIsBrowserDisplay

在派生类中重写时,获取一个值,该值指示浏览器客户端是否可以使用当前的编码显示内容。

System_CAPS_pubpropertyIsBrowserSave

在派生类中重写时,获取一个值,该值指示浏览器客户端是否可以使用当前的编码保存内容。

System_CAPS_pubpropertyIsMailNewsDisplay

在派生类中重写时,获取一个值,该值指示邮件和新闻客户端是否可以使用当前的编码显示内容。

System_CAPS_pubpropertyIsMailNewsSave

在派生类中重写时,获取一个值,该值指示邮件和新闻客户端是否可以使用当前的编码保存内容。

System_CAPS_pubpropertyIsReadOnly

在派生类中重写时,获取一个值,该值指示当前的编码是否为只读。

System_CAPS_pubpropertyIsSingleByte

在派生类中重写时,获取一个值,该值指示当前的编码是否使用单字节码位。

System_CAPS_pubpropertySystem_CAPS_staticUnicode

获取使用 Little-Endian 字节顺序的 UTF-16 格式的编码。

System_CAPS_pubpropertySystem_CAPS_staticUTF32

获取使用 Little-Endian 字节顺序的 UTF-32 格式的编码。

System_CAPS_pubpropertySystem_CAPS_staticUTF7

获取 UTF-7 格式的编码。

System_CAPS_pubpropertySystem_CAPS_staticUTF8

获取 UTF-8 格式的编码。

System_CAPS_pubpropertyWebName

在派生类中重写时,获取在 Internet 编号分配管理机构 (IANA) 注册的当前编码的名称。

System_CAPS_pubpropertyWindowsCodePage

在派生类中重写时,获取与当前编码最紧密对应的 Windows 操作系统代码页。

名称说明
System_CAPS_pubmethodClone()

当在派生类中重写时,创建当前 Encoding 对象的一个卷影副本。

System_CAPS_pubmethodSystem_CAPS_staticConvert(Encoding, Encoding, Byte[])

将整个字节数组从一种编码转换为另一种编码。

System_CAPS_pubmethodSystem_CAPS_staticConvert(Encoding, Encoding, Byte[], Int32, Int32)

将字节数组内某个范围的字节从一种编码转换为另一种编码。

System_CAPS_pubmethodEquals(Object)

确定指定的 Object 是否等同于当前实例。(覆盖 Object.Equals(Object)。)

System_CAPS_protmethodFinalize()

在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。(继承自 Object。)

System_CAPS_pubmethodGetByteCount(Char*, Int32)

在派生类中重写时,计算对一组字符(从指定的字符指针处开始)进行编码所产生的字节数。

System_CAPS_pubmethodGetByteCount(Char[])

在派生类中重写时,计算对指定字符数组中的所有字符进行编码所产生的字节数。

System_CAPS_pubmethodGetByteCount(Char[], Int32, Int32)

在派生类中重写时,计算对指定字符数组中的一组字符进行编码所产生的字节数。

System_CAPS_pubmethodGetByteCount(String)

在派生类中重写时,计算对指定字符串中的字符进行编码所产生的字节数。

System_CAPS_pubmethodGetBytes(Char*, Int32, Byte*, Int32)

在派生类中重写时,将一组字符(从指定的字符指针开始)编码为一个字节序列,并从指定的字节指针开始存储该字节序列。

System_CAPS_pubmethodGetBytes(Char[])

在派生类中重写时,将指定字符数组中的所有字符编码为一个字节序列。

System_CAPS_pubmethodGetBytes(Char[], Int32, Int32)

在派生类中重写时,将指定字符数组中的一组字符编码为一个字节序列。

System_CAPS_pubmethodGetBytes(Char[], Int32, Int32, Byte[], Int32)

在派生类中重写时,将指定字符数组中的一组字符编码为指定的字节数组。

System_CAPS_pubmethodGetBytes(String)

在派生类中重写时,将指定字符串中的所有字符编码为一个字节序列。

System_CAPS_pubmethodGetBytes(String, Int32, Int32, Byte[], Int32)

在派生类中重写时,将指定字符串中的一组字符编码为指定的字节数组。

System_CAPS_pubmethodGetCharCount(Byte*, Int32)

在派生类中重写时,计算对字节序列(从指定的字节指针开始)进行解码所产生的字符数。

System_CAPS_pubmethodGetCharCount(Byte[])

在派生类中重写时,计算对指定字节数组中的所有字节进行解码所产生的字符数。

System_CAPS_pubmethodGetCharCount(Byte[], Int32, Int32)

在派生类中重写时,计算对字节序列(从指定字节数组开始)进行解码所产生的字符数。

System_CAPS_pubmethodGetChars(Byte*, Int32, Char*, Int32)

在派生类中重写时,将一个字节序列(从指定的字节指针开始)解码为一组字符,并从指定的字符指针开始存储该组字符。

System_CAPS_pubmethodGetChars(Byte[])

在派生类中重写时,将指定字节数组中的所有字节解码为一组字符。

System_CAPS_pubmethodGetChars(Byte[], Int32, Int32)

在派生类中重写时,将指定字节数组中的一个字节序列解码为一组字符。

System_CAPS_pubmethodGetChars(Byte[], Int32, Int32, Char[], Int32)

在派生类中重写时,将指定字节数组中的字节序列解码为指定的字符数组。

System_CAPS_pubmethodGetDecoder()

在派生类中重写时,获取一个解码器,该解码器将已编码的字节序列转换为字符序列。

System_CAPS_pubmethodGetEncoder()

在派生类中重写时,获取一个解码器,该解码器将 Unicode 字符序列转换为已编码的字节序列。

System_CAPS_pubmethodSystem_CAPS_staticGetEncoding(Int32)

返回与指定代码页标识符关联的编码。

System_CAPS_pubmethodSystem_CAPS_staticGetEncoding(Int32, EncoderFallback, DecoderFallback)

返回与指定代码页标识符关联的编码。 参数指定一个错误处理程序,用于处理无法编码的字符和无法解码的字节序列。

System_CAPS_pubmethodSystem_CAPS_staticGetEncoding(String)

返回与指定代码页名称关联的编码。

System_CAPS_pubmethodSystem_CAPS_staticGetEncoding(String, EncoderFallback, DecoderFallback)

返回与指定代码页名称关联的编码。 参数指定一个错误处理程序,用于处理无法编码的字符和无法解码的字节序列。

System_CAPS_pubmethodSystem_CAPS_staticGetEncodings()

返回包含所有编码的数组。

System_CAPS_pubmethodGetHashCode()

返回当前实例的哈希代码。(覆盖 Object.GetHashCode()。)

System_CAPS_pubmethodGetMaxByteCount(Int32)

在派生类中重写时,计算对指定数目的字符进行编码所产生的最大字节数。

System_CAPS_pubmethodGetMaxCharCount(Int32)

在派生类中重写时,计算对指定数目的字节进行解码时所产生的最大字符数。

System_CAPS_pubmethodGetPreamble()

在派生类中重写时,返回指定所用编码的字节序列。

System_CAPS_pubmethodGetString(Byte*, Int32)

在派生类中重写时,将在指定地址开始的指定字节数解码为字符串。

System_CAPS_pubmethodGetString(Byte[])

在派生类中重写时,将指定字节数组中的所有字节解码为一个字符串。

System_CAPS_pubmethodGetString(Byte[], Int32, Int32)

在派生类中重写时,将指定字节数组中的一个字节序列解码为一个字符串。

System_CAPS_pubmethodGetType()

获取当前实例的 Type(继承自 Object。)

System_CAPS_pubmethodIsAlwaysNormalized()

使用默认范式获取一个值,该值指示当前编码是否始终被规范化。

System_CAPS_pubmethodIsAlwaysNormalized(NormalizationForm)

在派生类中重写时,使用指定范式获取一个值,该值指示当前编码是否始终被规范化。

System_CAPS_protmethodMemberwiseClone()

创建当前 Object 的浅表副本。(继承自 Object。)

System_CAPS_pubmethodSystem_CAPS_staticRegisterProvider(EncodingProvider)

注册编码提供程序。

System_CAPS_pubmethodToString()

返回表示当前对象的字符串。(继承自 Object。)

System_CAPS_note说明

To view the .NET Framework source code for this type, see the Reference Sourcehttp://referencesource.microsoft.com/#mscorlib/system/text/encoding.cs#3b6090c501893c25. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructionshttp://referencesource.microsoft.com/.

Encoding is the process of transforming a set of Unicode characters into a sequence of bytes. In contrast, decoding is the process of transforming a sequence of encoded bytes into a set of Unicode characters. For information about the Unicode Transformation Formats (UTFs) and other encodings supported by T:System.Text.Encoding, see Understanding Encodings.

Note that T:System.Text.Encoding is intended to operate on Unicode characters instead of arbitrary binary data, such as byte arrays. If you must encode arbitrary binary data into text, you should use a protocol such as uuencode, which is implemented by methods such as M:System.Convert.ToBase64CharArray(System.Byte[],System.Int32,System.Int32,System.Char[],System.Int32).

The .NET Framework provides the following implementations of the T:System.Text.Encoding class to support current Unicode encodings and other encodings:

  • ASCIIEncoding encodes Unicode characters as single 7-bit ASCII characters. This encoding only supports character values between U+0000 and U+007F. Code page 20127. Also available through the ASCII property.

  • UTF7Encoding encodes Unicode characters using the UTF-7 encoding. This encoding supports all Unicode character values. Code page 65000. Also available through the UTF7 property.

  • UTF8Encoding encodes Unicode characters using the UTF-8 encoding. This encoding supports all Unicode character values. Code page 65001. Also available through the UTF8 property.

  • UnicodeEncoding encodes Unicode characters using the UTF-16 encoding. Both little endian and big endian byte orders are supported. Also available through the Unicode property and the BigEndianUnicode property.

  • UTF32Encoding encodes Unicode characters using the UTF-32 encoding. Both little endian (code page 12000) and big endian (code page 12001) byte orders are supported. Also available through the UTF32 property.

The T:System.Text.Encoding class is primarily intended to convert between different encodings and Unicode. Often one of the derived Unicode classes is the correct choice for your app.

Use the M:System.Text.Encoding.GetEncoding(System.Int32) method to obtain other encodings, and call the M:System.Text.Encoding.GetEncodings method to get a list of all encodings.

The following table lists the encodings supported by the .NET Framework. It lists each encoding's code page number and the values of the encoding's P:System.Text.EncodingInfo.Name and P:System.Text.EncodingInfo.DisplayName properties. An asterisk in the last column indicates that the code page is natively supported by the .NET Framework, regardless of the underlying platform. Note that code pages whose P:System.Text.EncodingInfo.Name property corresponds to an international standard do not necessarily comply in full with that standard.

Code page

Name

Display name

.NET Framework support

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)

12000

utf-32

Unicode (UTF-32)

12001

utf-32BE

Unicode (UTF-32 Big endian)

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)

The following example calls the M:System.Text.Encoding.GetEncoding(System.Int32) and M:System.Text.Encoding.GetEncoding(System.String) methods to get the Greek (Windows) code page encoding. It compares the T:System.Text.Encoding objects returned by the method calls to show that they are equal, and then maps displays the Unicode code point and the corresponding code page value for each character in the Greek alphabet.

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      Encoding enc = Encoding.GetEncoding(1253);
      Encoding altEnc = Encoding.GetEncoding("windows-1253");
      Console.WriteLine("{0} = Code Page {1}: {2}", enc.EncodingName,
                        altEnc.CodePage, enc.Equals(altEnc));
      string greekAlphabet = "Α α Β β Γ γ Δ δ Ε ε Ζ ζ Η η " +
                             "Θ θ Ι ι Κ κ Λ λ Μ μ Ν ν Ξ ξ " +
                             "Ο ο Π π Ρ ρ Σ σ ς Τ τ Υ υ " +
                             "Φ φ Χ χ Ψ ψ Ω ω";
      Console.OutputEncoding = Encoding.UTF8;
      byte[] bytes = enc.GetBytes(greekAlphabet);
      Console.WriteLine("{0,-12} {1,20} {2,20:X2}", "Character",
                        "Unicode Code Point", "Code Page 1253");
      for (int ctr = 0; ctr < bytes.Length; ctr++) {
         if (greekAlphabet[ctr].Equals(' '))
            continue;

         Console.WriteLine("{0,-12} {1,20} {2,20:X2}", greekAlphabet[ctr],
                           GetCodePoint(greekAlphabet[ctr]), bytes[ctr]);
      }
   }

   private static string GetCodePoint(char ch)
   {
      string retVal = "u+";
      byte[] bytes = Encoding.Unicode.GetBytes(ch.ToString());
      for (int ctr = bytes.Length - 1; ctr >= 0; ctr--)
         retVal += bytes[ctr].ToString("X2");

      return retVal;
   }
}
// The example displays the following output:
//       Character      Unicode Code Point       Code Page 1253
//       Α                          u+0391                   C1
//       α                          u+03B1                   E1
//       Β                          u+0392                   C2
//       β                          u+03B2                   E2
//       Γ                          u+0393                   C3
//       γ                          u+03B3                   E3
//       Δ                          u+0394                   C4
//       δ                          u+03B4                   E4
//       Ε                          u+0395                   C5
//       ε                          u+03B5                   E5
//       Ζ                          u+0396                   C6
//       ζ                          u+03B6                   E6
//       Η                          u+0397                   C7
//       η                          u+03B7                   E7
//       Θ                          u+0398                   C8
//       θ                          u+03B8                   E8
//       Ι                          u+0399                   C9
//       ι                          u+03B9                   E9
//       Κ                          u+039A                   CA
//       κ                          u+03BA                   EA
//       Λ                          u+039B                   CB
//       λ                          u+03BB                   EB
//       Μ                          u+039C                   CC
//       μ                          u+03BC                   EC
//       Ν                          u+039D                   CD
//       ν                          u+03BD                   ED
//       Ξ                          u+039E                   CE
//       ξ                          u+03BE                   EE
//       Ο                          u+039F                   CF
//       ο                          u+03BF                   EF
//       Π                          u+03A0                   D0
//       π                          u+03C0                   F0
//       Ρ                          u+03A1                   D1
//       ρ                          u+03C1                   F1
//       Σ                          u+03A3                   D3
//       σ                          u+03C3                   F3
//       ς                          u+03C2                   F2
//       Τ                          u+03A4                   D4
//       τ                          u+03C4                   F4
//       Υ                          u+03A5                   D5
//       υ                          u+03C5                   F5
//       Φ                          u+03A6                   D6
//       φ                          u+03C6                   F6
//       Χ                          u+03A7                   D7
//       χ                          u+03C7                   F7
//       Ψ                          u+03A8                   D8
//       ψ                          u+03C8                   F8
//       Ω                          u+03A9                   D9
//       ω                          u+03C9                   F9

If the data to be converted is available only in sequential blocks (such as data read from a stream) or if the amount of data is so large that it needs to be divided into smaller blocks, you should use the T:System.Text.Decoder or the T:System.Text.Encoder provided by the M:System.Text.Encoding.GetDecoder method or the M:System.Text.Encoding.GetEncoder method, respectively, of a derived class.

The UTF-16 and the UTF-32 encoders can use the big endian byte order (most significant byte first) or the little endian byte order (least significant byte first). For example, the Latin Capital Letter A (U+0041) is serialized as follows (in hexadecimal):

  • UTF-16 big endian byte order: 00 41

  • UTF-16 little endian byte order: 41 00

  • UTF-32 big endian byte order: 00 00 00 41

  • UTF-32 little endian byte order: 41 00 00 00

It is generally more efficient to store Unicode characters using the native byte order. For example, it is better to use the little endian byte order on little endian platforms, such as Intel computers.

The M:System.Text.Encoding.GetPreamble method retrieves an array of bytes that includes the byte order mark (BOM). If this byte array is prefixed to an encoded stream, it helps the decoder to identify the encoding format used.

For more information on byte order and the byte order mark, see The Unicode Standard at the Unicode home pagehttp://go.microsoft.com/fwlink/?LinkId=37123.

Note that the encoding classes allow errors to:

  • Silently change to a "?" character.

  • Use a "best fit" character.

  • Change to an application-specific behavior through use of the T:System.Text.EncoderFallback and T:System.Text.DecoderFallback classes with the U+FFFD Unicode replacement character.

You should throw an exception on any data stream error. An app either uses a "throwonerror" flag when applicable or uses the T:System.Text.EncoderExceptionFallback and T:System.Text.DecoderExceptionFallback classes. Best fit fallback is often not recommended because it can cause data loss or confusion and is slower than simple character replacements. For ANSI encodings, the best fit behavior is the default.

The following example converts a string from one encoding to another.

System_CAPS_note说明

The byte[] array is the only type in this example that contains the encoded data. The .NET Char and String types are themselves Unicode, so the M:System.Text.Encoding.GetChars(System.Byte[],System.Int32,System.Int32,System.Char[],System.Int32) call decodes the data back to Unicode.

using System;
using System.Text;

class Example
{
   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 array.
      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.
      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);
   }
}
// The example displays the following output:
//    Original string: This string contains the unicode character Pi (Π)
//    Ascii converted string: This string contains the unicode character Pi (?)

通用 Windows 平台
自 8 起可用
.NET Framework
自 1.1 起可用
可移植类库
可移植 .NET 平台 中受支持
Silverlight
自 2.0 起可用
Windows Phone Silverlight
自 7.0 起可用
Windows Phone
自 8.1 起可用

此类型的所有公共静态(Visual Basic 中的 已共享 在 Visual Basic 中)成员都是线程安全的。不保证所有实例成员都是线程安全的。

返回页首
显示: