Encoding 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示字符编码。
public ref class Encoding abstractpublic ref class Encoding abstract : ICloneablepublic abstract class Encodingpublic abstract class Encoding : ICloneable[System.Serializable]
public abstract class Encoding[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Encoding : ICloneabletype Encoding = classtype Encoding = class
    interface ICloneable[<System.Serializable>]
type Encoding = class[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Encoding = class
    interface ICloneablePublic MustInherit Class EncodingPublic MustInherit Class Encoding
Implements ICloneable- 继承
- 
				Encoding
- 派生
- 属性
- 实现
示例
下面的示例将字符串从一种编码转换为另一种编码。
注意
              byte[]在此示例中,数组是包含编码数据的唯一类型。 .NET Char 和 String 类型本身就是 unicode,因此调用会将 GetChars 数据解码回 unicode。
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 array.
   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.
   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 );
}
// 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 (?)
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 (?)
Imports System.Text
Class Example
   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 array.
      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 array into a char array and then into a string.
      Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)-1) 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
' 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 (?)
注解
有关此 API 的详细信息,请参阅 编码的补充 API 说明。
构造函数
| Encoding() | 初始化 Encoding 类的新实例。 | 
| Encoding(Int32) | 初始化对应于指定代码页的 Encoding 类的新实例。 | 
| Encoding(Int32, EncoderFallback, DecoderFallback) | 使用指定编码器和解码器回退策略初始化对应于指定代码页的 Encoding 类的新实例。 | 
属性
| ASCII | 获取 ASCII(7 位)字符集的编码。 | 
| BigEndianUnicode | 获取使用 Big Endian 字节顺序的 UTF-16 格式的编码。 | 
| BodyName | 在派生类中重写时,获取可与邮件代理正文标记一起使用的当前编码的名称。 | 
| CodePage | 在派生类中重写时,获取当前 Encoding 的代码页标识符。 | 
| DecoderFallback | 获取或设置当前 DecoderFallback 对象的 Encoding 对象。 | 
| Default | 获取此 .NET 实现的默认编码。 | 
| EncoderFallback | 获取或设置当前 EncoderFallback 对象的 Encoding 对象。 | 
| EncodingName | 在派生类中重写时,获取当前编码的用户可读说明。 | 
| HeaderName | 在派生类中重写时,获取可与邮件代理头标记一起使用的当前编码的名称。 | 
| IsBrowserDisplay | 在派生类中重写时,获取一个值,该值指示浏览器客户端是否可以使用当前的编码显示内容。 | 
| IsBrowserSave | 在派生类中重写时,获取一个值,该值指示浏览器客户端是否可以使用当前的编码保存内容。 | 
| IsMailNewsDisplay | 在派生类中重写时,获取一个值,该值指示邮件和新闻客户端是否可以使用当前的编码显示内容。 | 
| IsMailNewsSave | 在派生类中重写时,获取一个值,该值指示邮件和新闻客户端是否可以使用当前的编码保存内容。 | 
| IsReadOnly | 在派生类中重写时,获取一个值,该值指示当前的编码是否为只读。 | 
| IsSingleByte | 在派生类中重写时,获取一个值,该值指示当前的编码是否使用单字节码位。 | 
| Latin1 | 获取 Latin1 字符集 (ISO-8859-1) 的编码。 | 
| Preamble | 在派生类中重写时,返回包含指定所用编码的字节序列的范围。 | 
| Unicode | 获取使用 Little-Endian 字节顺序的 UTF-16 格式的编码。 | 
| UTF32 | 获取使用 Little-Endian 字节顺序的 UTF-32 格式的编码。 | 
| UTF7 | 
				已过时.
			 获取 UTF-7 格式的编码。 | 
| UTF8 | 获取 UTF-8 格式的编码。 | 
| WebName | 在派生类中重写时,获取在 Internet 编号分配管理机构 (IANA) 注册的当前编码的名称。 | 
| WindowsCodePage | 在派生类中重写时,获取与当前编码最紧密对应的 Windows 操作系统代码页。 | 
方法
| Clone() | 当在派生类中重写时,创建当前 Encoding 对象的一个卷影副本。 | 
| Convert(Encoding, Encoding, Byte[]) | 将整个字节数组从一种编码转换为另一种编码。 | 
| Convert(Encoding, Encoding, Byte[], Int32, Int32) | 将字节数组内某个范围的字节从一种编码转换为另一种编码。 | 
| CreateTranscodingStream(Stream, Encoding, Encoding, Boolean) | 创建一个 Stream,用于在内部 Encoding 和外部 Encoding 之间进行数据转码,类似于 Convert(Encoding, Encoding, Byte[])。 | 
| Equals(Object) | 确定指定的 Object 是否等同于当前实例。 | 
| GetByteCount(Char*, Int32) | 在派生类中重写时,计算对一组字符(从指定的字符指针处开始)进行编码所产生的字节数。 | 
| GetByteCount(Char[]) | 在派生类中重写时,计算对指定字符数组中的所有字符进行编码所产生的字节数。 | 
| GetByteCount(Char[], Int32, Int32) | 在派生类中重写时,计算对指定字符数组中的一组字符进行编码所产生的字节数。 | 
| GetByteCount(ReadOnlySpan<Char>) | 在派生类中重写时,计算对指定字符范围的字符进行编码所产生的字节数。 | 
| GetByteCount(String) | 在派生类中重写时,计算对指定字符串中的字符进行编码所产生的字节数。 | 
| GetByteCount(String, Int32, Int32) | 在派生类中重写时,计算对指定字符串中的一组字符进行编码所产生的字节数。 | 
| GetBytes(Char*, Int32, Byte*, Int32) | 在派生类中重写时,将一组字符(从指定的字符指针开始)编码为一个字节序列,并从指定的字节指针开始存储该字节序列。 | 
| GetBytes(Char[]) | 在派生类中重写时,将指定字符数组中的所有字符编码为一个字节序列。 | 
| GetBytes(Char[], Int32, Int32) | 在派生类中重写时,将指定字符数组中的一组字符编码为一个字节序列。 | 
| GetBytes(Char[], Int32, Int32, Byte[], Int32) | 在派生类中重写时,将指定字符数组中的一组字符编码为指定的字节数组。 | 
| GetBytes(ReadOnlySpan<Char>, Span<Byte>) | 在派生类中重写时,将指定只读范围中的一组字符编码为字节范围。 | 
| GetBytes(String) | 在派生类中重写时,将指定字符串中的所有字符编码为一个字节序列。 | 
| GetBytes(String, Int32, Int32) | 在派生类中重写时,从指定的  | 
| GetBytes(String, Int32, Int32, Byte[], Int32) | 在派生类中重写时,将指定字符串中的一组字符编码为指定的字节数组。 | 
| GetCharCount(Byte*, Int32) | 在派生类中重写时,计算对字节序列(从指定的字节指针开始)进行解码所产生的字符数。 | 
| GetCharCount(Byte[]) | 在派生类中重写时,计算对指定字节数组中的所有字节进行解码所产生的字符数。 | 
| GetCharCount(Byte[], Int32, Int32) | 在派生类中重写时,计算对字节序列(从指定字节数组开始)进行解码所产生的字符数。 | 
| GetCharCount(ReadOnlySpan<Byte>) | 在派生类中重写时,计算对提供的只读字节范围进行解码所产生的字符数。 | 
| GetChars(Byte*, Int32, Char*, Int32) | 在派生类中重写时,将一个字节序列(从指定的字节指针开始)解码为一组字符,并从指定的字符指针开始存储该组字符。 | 
| GetChars(Byte[]) | 在派生类中重写时,将指定字节数组中的所有字节解码为一组字符。 | 
| GetChars(Byte[], Int32, Int32) | 在派生类中重写时,将指定字节数组中的一个字节序列解码为一组字符。 | 
| GetChars(Byte[], Int32, Int32, Char[], Int32) | 在派生类中重写时,将指定字节数组中的字节序列解码为指定的字符数组。 | 
| GetChars(ReadOnlySpan<Byte>, Span<Char>) | 在派生类中重写时,将指定只读字节范围中的所有字节解码为字符范围。 | 
| GetDecoder() | 在派生类中重写时,获取一个解码器,该解码器将已编码的字节序列转换为字符序列。 | 
| GetEncoder() | 在派生类中重写时,获取一个解码器,该解码器将 Unicode 字符序列转换为已编码的字节序列。 | 
| GetEncoding(Int32) | 返回与指定代码页标识符关联的编码。 | 
| GetEncoding(Int32, EncoderFallback, DecoderFallback) | 返回与指定代码页标识符关联的编码。 参数指定一个错误处理程序,用于处理无法编码的字符和无法解码的字节序列。 | 
| GetEncoding(String) | 返回与指定代码页名称关联的编码。 | 
| GetEncoding(String, EncoderFallback, DecoderFallback) | 返回与指定代码页名称关联的编码。 参数指定一个错误处理程序,用于处理无法编码的字符和无法解码的字节序列。 | 
| GetEncodings() | 返回包含所有编码的数组。 | 
| GetHashCode() | 返回当前实例的哈希代码。 | 
| GetMaxByteCount(Int32) | 在派生类中重写时,计算对指定数目的字符进行编码所产生的最大字节数。 | 
| GetMaxCharCount(Int32) | 在派生类中重写时,计算对指定数目的字节进行解码时所产生的最大字符数。 | 
| GetPreamble() | 在派生类中重写时,返回指定所用编码的字节序列。 | 
| GetString(Byte*, Int32) | 在派生类中重写时,将在指定地址开始的指定字节数解码为字符串。 | 
| GetString(Byte[]) | 在派生类中重写时,将指定字节数组中的所有字节解码为一个字符串。 | 
| GetString(Byte[], Int32, Int32) | 在派生类中重写时,将指定字节数组中的一个字节序列解码为一个字符串。 | 
| GetString(ReadOnlySpan<Byte>) | 在派生类中重写时,将指定字节范围中的所有字节解码为一个字符串。 | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IsAlwaysNormalized() | 使用默认范式获取一个值,该值指示当前编码是否始终被规范化。 | 
| IsAlwaysNormalized(NormalizationForm) | 在派生类中重写时,使用指定范式获取一个值,该值指示当前编码是否始终被规范化。 | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| RegisterProvider(EncodingProvider) | 注册编码提供程序。 | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
| TryGetBytes(ReadOnlySpan<Char>, Span<Byte>, Int32) | 如果目标足够大,则从指定的只读范围将一组字符编码为字节范围。 | 
| TryGetChars(ReadOnlySpan<Byte>, Span<Char>, Int32) | 如果目标足够大,则从指定的只读范围将一组字节解码为字符范围。 | 
扩展方法
| GetBytes(Encoding, ReadOnlySequence<Char>) | 使用指定的 Encoding 将指定的 ReadOnlySequence<T> 编码到 Byte 数组中。 | 
| GetBytes(Encoding, ReadOnlySequence<Char>, IBufferWriter<Byte>) | 使用指定的 Encoding 将指定的 ReadOnlySequence<T> 解码为  | 
| GetBytes(Encoding, ReadOnlySequence<Char>, Span<Byte>) | 使用指定的 Encoding 将指定的 ReadOnlySequence<T> 编码为  | 
| GetBytes(Encoding, ReadOnlySpan<Char>, IBufferWriter<Byte>) | 使用指定的 Encoding 将指定的 ReadOnlySpan<T> 编码为  | 
| GetChars(Encoding, ReadOnlySequence<Byte>, IBufferWriter<Char>) | 使用指定的 Encoding 将指定的 ReadOnlySequence<T> 解码为  | 
| GetChars(Encoding, ReadOnlySequence<Byte>, Span<Char>) | 使用指定的 Encoding 将指定的 ReadOnlySequence<T> 解码为  | 
| GetChars(Encoding, ReadOnlySpan<Byte>, IBufferWriter<Char>) | 使用指定的 Encoding 将指定的 ReadOnlySpan<T> 解码为  | 
| GetString(Encoding, ReadOnlySequence<Byte>) | 使用指定的 Encoding 将指定的 ReadOnlySequence<T> 解码到 String。 |