Decoder 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将已编码字节的序列转换为一组字符。
public ref class Decoder abstractpublic abstract class Decoder[System.Serializable]
public abstract class Decoder[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Decodertype Decoder = class[<System.Serializable>]
type Decoder = class[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Decoder = classPublic MustInherit Class Decoder- 继承
- 
				Decoder
- 属性
示例
下面的示例演示如何使用 Decoder 将两个不同的字节数组转换为字符数组。 字符的字节之一跨越数组。 这类似于 StreamReader 对象在读取流时在内部执行的操作。
using namespace System;
using namespace System::Text;
int main()
{
   
   // These bytes in UTF-8 correspond to 3 different Unicode
   // characters: space (U+0020), # (U+0023), and the biohazard
   // symbol (U+2623).  Note the biohazard symbol requires 3 bytes
   // in UTF-8 (hexadecimal e2, 98, a3).  Decoders store state across
   // multiple calls to GetChars, handling the case when one char
   // is in multiple byte arrays.
   array<Byte>^bytes1 = {0x20,0x23,0xe2};
   array<Byte>^bytes2 = {0x98,0xa3};
   array<Char>^chars = gcnew array<Char>(3);
   Decoder^ d = Encoding::UTF8->GetDecoder();
   int charLen = d->GetChars( bytes1, 0, bytes1->Length, chars, 0 );
   
   // The value of charLen should be 2 now.
   charLen += d->GetChars( bytes2, 0, bytes2->Length, chars, charLen );
   for ( UInt16 index(0); index < chars->Length; ++index )
   {
      Console::Write( "U+{0:X4}  ", static_cast<UInt16>(chars[ index ]) );
   }
}
using System;
using System.Text;
public class dec
{
    public static void Main()
    {
        // These bytes in UTF-8 correspond to 3 different Unicode
        // characters: space (U+0020), # (U+0023), and the biohazard
        // symbol (U+2623).  Note the biohazard symbol requires 3 bytes
        // in UTF-8 (hexadecimal e2, 98, a3).  Decoders store state across
        // multiple calls to GetChars, handling the case when one char
        // is in multiple byte arrays.
        byte[] bytes1 = { 0x20, 0x23, 0xe2 };
        byte[] bytes2 = { 0x98, 0xa3 };
        char[] chars = new char[3];
        Decoder d = Encoding.UTF8.GetDecoder();
        int charLen = d.GetChars(bytes1, 0, bytes1.Length, chars, 0);
        // The value of charLen should be 2 now.
        charLen += d.GetChars(bytes2, 0, bytes2.Length, chars, charLen);
        foreach(char c in chars)
            Console.Write("U+{0:X4}  ", (ushort)c);
    }
}
Imports System.Text
Public Class dec
    
    Public Shared Sub Main()
        ' These bytes in UTF-8 correspond to 3 different Unicode
        ' characters: space (U+0020), # (U+0023), and the biohazard
        ' symbol (U+2623).  Note the biohazard symbol requires 3 bytes
        ' in UTF-8 (hexadecimal e2, 98, a3).  Decoders store state across
        ' multiple calls to GetChars, handling the case when one char
        ' is in multiple byte arrays.
        Dim bytes1 As Byte() =  {&H20, &H23, &HE2}
        Dim bytes2 As Byte() =  {&H98, &HA3}
        Dim chars(3) As Char
        
        Dim d As Decoder = Encoding.UTF8.GetDecoder()
        Dim charLen As Integer = d.GetChars(bytes1, 0, bytes1.Length, chars, 0)
        ' The value of charLen should be 2 now.
        charLen += d.GetChars(bytes2, 0, bytes2.Length, chars, charLen)
        Dim c As Char
        For Each c In  chars
            Console.Write("U+{0:X4}  ", Convert.ToUInt16(c) )
        Next c
    End Sub
End Class
注解
若要获取 类实现的 Decoder 实例,请调用 GetDecoder 实现的 Encoding 方法。
              GetCharCount方法确定多少个字符会导致对一个字节序列进行解码,并且该 GetChars 方法执行实际解码。 类中提供了这两种方法的 Decoder 多个版本。 有关详细信息,请参阅 Encoding.GetChars。 
              Decoder对象维护对 GetChars 或 Convert 方法的连续调用之间的状态信息,以便它可以正确解码跨块的字节序列。 
              Decoder还保留数据块末尾的尾随字节,并在下一个解码操作中使用尾随字节。 因此, GetDecoder 和 GetEncoder 对于网络传输和文件操作很有用,因为这些操作通常处理数据块而不是完整的数据流。
注意
当应用程序完成数据流时,它应通过在适当的方法调用中将 参数设置为 flushtrue 来确保刷新状态信息。 如果发生异常或应用程序切换流,则应调用 Reset 以清除对象的内部状态 Decoder 。
实施者说明
当应用程序从此类继承时,它必须重写所有成员。
构造函数
| Decoder() | 初始化 Decoder 类的新实例。 | 
属性
| Fallback | 获取或设置当前 DecoderFallback 对象的 Decoder 对象。 | 
| FallbackBuffer | 获取与当前 DecoderFallbackBuffer 对象关联的 Decoder 对象。 | 
方法
| Convert(Byte*, Int32, Char*, Int32, Boolean, Int32, Int32, Boolean) | 将编码字节的缓冲区转换为 UTF-16 编码字符,并将结果存储在另一个缓冲区中。 | 
| Convert(Byte[], Int32, Int32, Char[], Int32, Int32, Boolean, Int32, Int32, Boolean) | 将编码字节的数组转换为 UTF-16 编码字符,并将结果存储在字符数组中。 | 
| Convert(ReadOnlySpan<Byte>, Span<Char>, Boolean, Int32, Int32, Boolean) | 将编码字节的范围转换为 UTF-16 编码字符,并将结果存储在另一个范围缓冲区中。 | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetCharCount(Byte*, Int32, Boolean) | 在派生类中重写时,计算对字节序列(从指定的字节指针开始)进行解码所产生的字符数。 一个参数,该参数指示是否在计算后清除解码器的内部状态。 | 
| GetCharCount(Byte[], Int32, Int32) | 在派生类中重写时,计算对字节序列(从指定字节数组开始)进行解码所产生的字符数。 | 
| GetCharCount(Byte[], Int32, Int32, Boolean) | 在派生类中重写时,计算对字节序列(从指定字节数组开始)进行解码所产生的字符数。 一个参数,该参数指示是否在计算后清除解码器的内部状态。 | 
| GetCharCount(ReadOnlySpan<Byte>, Boolean) | 在派生类中重写时,计算对范围中字节序列进行解码所产生的字符数。 一个参数,该参数指示是否在计算后清除解码器的内部状态。 | 
| GetChars(Byte*, Int32, Char*, Int32, Boolean) | 在派生类中重写时,将字节序列(从指定的字节指针处开始)和任何内部缓冲区中的字节解码为从指定字符指针开始存储的一组字符。 一个参数,指示转换后是否要清除解码器的内部状态。 | 
| GetChars(Byte[], Int32, Int32, Char[], Int32) | 在派生类中重写时,将指定字节数组的字节序列和内部缓冲区中的任何字节解码到指定的字符数组。 | 
| GetChars(Byte[], Int32, Int32, Char[], Int32, Boolean) | 在派生类中重写时,将指定字节数组的字节序列和内部缓冲区中的任何字节解码到指定的字符数组。 一个参数,指示转换后是否要清除解码器的内部状态。 | 
| GetChars(ReadOnlySpan<Byte>, Span<Char>, Boolean) | 在派生类中重写时,将范围字节序列和内部缓冲区中的任何字节解码为从指定字符指针开始存储的一组字符。 一个参数,指示转换后是否要清除解码器的内部状态。 | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| Reset() | 在派生类中重写时,将解码器设置回它的初始状态。 | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
扩展方法
| Convert(Decoder, ReadOnlySequence<Byte>, IBufferWriter<Char>, Boolean, Int64, Boolean) | 将 ReadOnlySequence<T> 转换为 UTF-16 编码字符,并将结果写入  | 
| Convert(Decoder, ReadOnlySpan<Byte>, IBufferWriter<Char>, Boolean, Int64, Boolean) | 使用  |