Decoder 類別

定義

將編碼的位元組序列轉換成一組字元。

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 將兩個不同的位元組陣列轉換成字元陣列。 其中一個字元的位元組會跨越數位。 這類似於 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.GetCharsDecoder物件會在連續呼叫 GetCharsConvert 方法之間維護狀態資訊,以便正確地譯碼跨越區塊的位元組序列。 Decoder也會在數據區塊結尾保留尾端的位元組,並在下一個譯碼作業中使用尾端位元組。 因此, GetDecoderGetEncoder 對於網路傳輸和檔案作業很有用,因為這些作業通常會處理數據區塊,而不是完整的數據流。

注意

當應用程式使用數據流完成時,應該在適當的方法呼叫中將 參數設定 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 編碼的字元,並將結果寫入到 writer

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

使用 decoder 來將 ReadOnlySpan<T> 轉換成字元,並將結果寫入到 writer

適用於

另請參閱