Decoder 类

将一个编码字节序列转换为一组字符。

**命名空间:**System.Text
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public MustInherit Class Decoder
用法
Dim instance As Decoder
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public abstract class Decoder
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class Decoder abstract
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public abstract class Decoder
SerializableAttribute 
ComVisibleAttribute(true) 
public abstract class Decoder

备注

编码是将一组字符转换为一个字节序列的过程。解码是一个反向操作过程,即将一个编码字节序列转换为一组字符。

Decoder 在连续调用 GetChars 之间维护状态信息,因此它可以正确地对跨块的字节序列进行解码。Decoder 还保留数据块结尾的尾部字节并将这些尾部字节用在下一次解码操作中。因此,GetDecoderGetEncoder 在网络传输和文件操作中很有用,这是因为这些操作通常处理数据块而不是完整的数据流。

GetCharCount 方法确定有多少字符导致对字节序列进行解码,而 GetChars 方法执行实际的解码。

若要获得此类某个实现的实例,请使用 Encoding 实现的 GetDecoder 方法。

版本注意事项

转换操作期间可以序列化 DecoderEncoder 对象。如果在相同版本的 .NET Framework 中反序列化对象,则保留对象的状态,但如果在另一个版本中反序列化对象,则对象的状态会丢失。

给继承者的说明 从此类继承时,您必须重写所有成员。

示例

下面的代码示例说明如何使用 Decoder 将两个不同的字节数组转换为一个字符数组。字符的其中一个字节跨越两个数组。这与 System.IO.StreamReader 读取流时在内部所执行的操作相似。

Imports System
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
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);
    }
}
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 ]) );

   }
}
import System.*;
import System.Text.*;

public class Dec
{
    public static void main(String[] args)
    {
        // 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.
        ubyte bytes1[] =  { 0x20, 0x23, 0xE2 };
        ubyte bytes2[] =  { 0x98, 0xA3 };
        char chars[] = new char[3];
        
        Decoder d = Encoding.get_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 (int iCtr = 0; iCtr < chars.length; iCtr++) {
            char c = chars[iCtr];
            Console.Write("U+{0}  ",((Int16)c).ToString("X4"));
        }
    } //main
} //Dec

继承层次结构

System.Object
  System.Text.Decoder

线程安全

此类型的任何公共静态(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

请参见

参考

Decoder 成员
System.Text 命名空间
Encoder
Encoding