Decoder 類別

定義

將編碼的位元組序列轉換成一組字元。Converts a sequence of encoded bytes into a set of characters.

public ref class Decoder abstract
public abstract class Decoder
[System.Serializable]
public abstract class Decoder
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Decoder
type Decoder = class
[<System.Serializable>]
type Decoder = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Decoder = class
Public MustInherit Class Decoder
繼承
Decoder
屬性

範例

下列範例示範 Decoder 如何使用,將兩個不同的位元組陣列轉換成字元陣列。The following example demonstrates the use of a Decoder to convert two different byte arrays into a character array. 其中一個字元的位元組會跨越陣列。One of the character's bytes spans the arrays. 這類似于在 StreamReader 讀取資料流程時,物件會在內部執行的工作。This is similar to what a StreamReader object does internally when reading a stream.

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 執行的方法 EncodingTo obtain an instance of an implementation of the Decoder class, the application should use the GetDecoder method of an Encoding implementation.

GetCharCount方法會判斷產生一連串位元組的字元數,而且 GetChars 方法會執行實際的解碼。The GetCharCount method determines how many characters result in decoding a sequence of bytes, and the GetChars method performs the actual decoding. 這兩種方法都可以在類別中使用 DecoderThere are several versions of both of these methods available in the Decoder class. 如需詳細資訊,請參閱Encoding.GetCharsFor more information, see Encoding.GetChars. Decoder物件會在連續呼叫或方法之間維護狀態資訊 GetChars Convert ,讓它可以正確地解碼跨越區塊的位元組序列。A Decoder object maintains state information between successive calls to GetChars or Convert methods so it can correctly decode byte sequences that span blocks. Decoder也會保留資料區塊結尾的尾端位元組,並在下一個解碼作業中使用尾端的位元組。The Decoder also preserves trailing bytes at the end of data blocks and uses the trailing bytes in the next decoding operation. 因此, GetDecoder 和適用 GetEncoder 于網路傳輸和檔案作業,因為這些作業通常會處理資料區塊,而不是完整的資料流程。Therefore, GetDecoder and GetEncoder are useful for network transmission and file operations because those operations often deal with blocks of data instead of a complete data stream.

注意

當應用程式使用資料流程完成時,應該 flush true 在適當的方法呼叫中將參數設定為,以確定狀態資訊已排清。When the application is done with a stream of data, it should make sure that the state information is flushed by setting the flush parameter to true in the appropriate method call. 如果發生例外狀況,或應用程式切換資料流程,則應該呼叫 Reset 以清除物件的內部狀態 DecoderIf an exception occurs or if the application switches streams, it should call Reset to clear the internal state of the Decoder object.

版本考慮Version Considerations

DecoderEncoder 物件可在轉換作業期間進行序列化。A Decoder or Encoder object can be serialized during a conversion operation. 如果物件在相同版本的 .NET Framework 中還原序列化,則會保留該物件的狀態,但如果在另一個版本還原序列化,則會遺失。The state of the object is retained if it is deserialized in the same version of the .NET Framework, but lost if it is deserialized in another version.

給實施者的注意事項

當您的應用程式繼承自這個類別時,它必須覆寫所有成員。When your application inherits from this class, it must override all the members.

建構函式

Decoder()

初始化 Decoder 類別的新執行個體。Initializes a new instance of the Decoder class.

屬性

Fallback

取得或設定目前 DecoderFallback 物件的 Decoder 物件。Gets or sets a DecoderFallback object for the current Decoder object.

FallbackBuffer

取得與目前 DecoderFallbackBuffer 物件關聯的 Decoder 物件。Gets the DecoderFallbackBuffer object associated with the current Decoder object.

方法

Convert(Byte*, Int32, Char*, Int32, Boolean, Int32, Int32, Boolean)

將已編碼位元組的緩衝區轉換成 UTF-16 編碼的字元,並將結果儲存在另一個緩衝區。Converts a buffer of encoded bytes to UTF-16 encoded characters and stores the result in another buffer.

Convert(Byte[], Int32, Int32, Char[], Int32, Int32, Boolean, Int32, Int32, Boolean)

將已編碼位元組的陣列轉換成 UTF-16 編碼的字元,並將結果儲存在另一個字元陣列中。Converts an array of encoded bytes to UTF-16 encoded characters and stores the result in a character array.

Convert(ReadOnlySpan<Byte>, Span<Char>, Boolean, Int32, Int32, Boolean)

將編碼位元組的範圍轉換為 UTF-16 編碼字元,並將結果儲存在另一個範圍緩衝區中。Converts a span of encoded bytes to UTF-16 encoded characters and stores the result in another span buffer.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetCharCount(Byte*, Int32, Boolean)

在衍生類別中覆寫時,計算從指定的位元組指標開始,解碼位元組序列所產生的字元數目。When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes starting at the specified byte pointer. 參數,指出計算後是否要清除解碼器的內部狀態。A parameter indicates whether to clear the internal state of the decoder after the calculation.

GetCharCount(Byte[], Int32, Int32)

在衍生類別中覆寫時,計算從指定的位元組陣列解碼位元組序列所產生的字元數目。When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.

GetCharCount(Byte[], Int32, Int32, Boolean)

在衍生類別中覆寫時,計算從指定的位元組陣列解碼位元組序列所產生的字元數目。When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. 參數,指出計算後是否要清除解碼器的內部狀態。A parameter indicates whether to clear the internal state of the decoder after the calculation.

GetCharCount(ReadOnlySpan<Byte>, Boolean)

在衍生類別中覆寫時,計算藉由解碼範圍中位元組序列所產生的字元數。When overridden in a derived class, calculates the number of characters produced by decoding the sequence of bytes in the span. 參數,指出計算後是否要清除解碼器的內部狀態。A parameter indicates whether to clear the internal state of the decoder after the calculation.

GetChars(Byte*, Int32, Char*, Int32, Boolean)

在衍生類別中覆寫時,會將由指定位元組指標開始的位元組序列以及內部緩衝區內的任何位元組,解碼成一組字元 (會從指定的字元指標開始存放這些字元)。When overridden in a derived class, decodes a sequence of bytes starting at the specified byte pointer and any bytes in the internal buffer into a set of characters that are stored starting at the specified character pointer. 參數會指示,在轉換之後是否要清除解碼器的內部狀態。A parameter indicates whether to clear the internal state of the decoder after the conversion.

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

在衍生類別中覆寫時,將指定位元組陣列中的位元組序列以及內部緩衝區內的任何位元組,解碼成指定的字元陣列。When overridden in a derived class, decodes a sequence of bytes from the specified byte array and any bytes in the internal buffer into the specified character array.

GetChars(Byte[], Int32, Int32, Char[], Int32, Boolean)

在衍生類別中覆寫時,將指定位元組陣列中的位元組序列以及內部緩衝區內的任何位元組,解碼成指定的字元陣列。When overridden in a derived class, decodes a sequence of bytes from the specified byte array and any bytes in the internal buffer into the specified character array. 參數會指示,在轉換之後是否要清除解碼器的內部狀態。A parameter indicates whether to clear the internal state of the decoder after the conversion.

GetChars(ReadOnlySpan<Byte>, Span<Char>, Boolean)

在衍生類別中覆寫時,會將範圍位元組序列和內部緩衝區中的任何位元組解碼成從指定字元指標開始儲存的字元集。When overridden in a derived class, decodes a sequence of span bytes and any bytes in the internal buffer into a set of characters that are stored starting at the specified character pointer. 參數會指示,在轉換之後是否要清除解碼器的內部狀態。A parameter indicates whether to clear the internal state of the decoder after the conversion.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
Reset()

在衍生類別中覆寫時,將解碼器設定回其初始狀態。When overridden in a derived class, sets the decoder back to its initial state.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

擴充方法

Convert(Decoder, ReadOnlySequence<Byte>, IBufferWriter<Char>, Boolean, Int64, Boolean)

ReadOnlySequence<T> 轉換成 UTF-16 編碼的字元,並將結果寫入到 writerConverts a ReadOnlySequence<T> to UTF-16 encoded characters and writes the result to writer.

Convert(Decoder, ReadOnlySpan<Byte>, IBufferWriter<Char>, Boolean, Int64, Boolean)

使用 decoder 來將 ReadOnlySpan<T> 轉換成字元,並將結果寫入到 writerConverts a ReadOnlySpan<T> to chars using decoder and writes the result to writer.

適用於

另請參閱