語言

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
屬性

範例

以下範例示範如何使用 a Decoder 將兩個不同的位元組陣列轉換成字元陣列。 字元的一個位元組跨跨陣列。 這類似於物件在內部讀取串流時的行為 StreamReader 。

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 對於網路傳輸與檔案操作非常有用,因為這些操作通常處理的是資料區塊,而非完整的資料串流。

Note

當應用程式處理一串資料時,應確保狀態資訊被清除,方法是在適當的方法呼叫中設定flush參數為 。true 若發生例外或應用程式切換串流,應呼叫 Reset 以清除物件的內部狀態 Decoder 。

給實施者的注意事項

當你的應用程式繼承自此類別時,必須覆蓋所有成員。

建構函式

名稱 Description
Decoder()

初始化 Decoder 類別的新執行個體。

屬性

名稱 Description
Fallback

取得或設定DecoderFallback一個物件。Decoder

FallbackBuffer

取得 DecoderFallbackBuffer 與當前 Decoder 物件相關聯的物件。

方法

名稱 Description
Convert(Byte[], Int32, Int32, Char[], Int32, Int32, Boolean, Int32, Int32, Boolean)

將編碼的位元組陣列轉換為 UTF-16 編碼字元,並將結果儲存在字元陣列中。

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

將編碼位元組的緩衝區轉換為 UTF-16 編碼字元,並將結果儲存在另一個緩衝區中。

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

將一段編碼的位元組轉換成 UTF-16 編碼字元,並將結果儲存在另一個區間緩衝區。

Equals(Object)

判斷指定的物件是否等於目前的物件。

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

當在派生類別中覆寫時,會計算從指定位元組陣列解碼一連串位元組所產生的字元數。 一個參數表示計算後是否要清除解碼器的內部狀態。

GetCharCount(Byte[], Int32, Int32)

當在派生類別中覆寫時,會計算從指定位元組陣列解碼一連串位元組所產生的字元數。

GetCharCount(Byte*, Int32, Boolean)

當在派生類別中覆寫時,會計算從指定位元組指標開始解碼一串位元組所產生的字元數。 一個參數表示計算後是否要清除解碼器的內部狀態。

GetCharCount(ReadOnlySpan<Byte>, Boolean)

當在衍生類別中覆寫時,會透過解碼該區間的位元組序列來計算出多少字元。 一個參數表示計算後是否要清除解碼器的內部狀態。

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

當在派生類別中覆寫時,會將指定位元組陣列中的一串位元組及內部緩衝區的位元組解碼成指定的字元陣列。 一個參數指示轉換後是否要清除解碼器的內部狀態。

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

當在派生類別中覆寫時,會將指定位元組陣列中的一串位元組及內部緩衝區的位元組解碼成指定的字元陣列。

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

當在派生類別中覆寫時,會將從指定位元組指標開始的一串位元組及內部緩衝區中的位元組解碼成一組字元,這些字元從指定字元指標開始儲存。 一個參數指示轉換後是否要清除解碼器的內部狀態。

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

當在衍生類別中覆寫時,會將一串跨度位元組及內部緩衝區中的任何位元組解碼成一組字元,從指定的字元指標開始儲存。 一個參數指示轉換後是否要清除解碼器的內部狀態。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
Reset()

當在衍生類別中覆寫時,會將解碼器回到初始狀態。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

擴充方法

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

將 a ReadOnlySequence<T> 轉換為 UTF-16 編碼字元,並將結果寫入 writer。

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

將 a ReadOnlySpan<T> 轉換成字元 ,並 decoder 將結果寫入 writer。

適用於

另請參閱