UTF7Encoding.GetDecoder 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得一個解碼器,能將 UTF-7 編碼的位元組序列轉換為 Unicode 字元序列。
public:
override System::Text::Decoder ^ GetDecoder();
public override System.Text.Decoder GetDecoder();
override this.GetDecoder : unit -> System.Text.Decoder
Public Overrides Function GetDecoder () As Decoder
傳回
Decoder A 將 UTF-7 編碼的位元組序列轉換為 Unicode 字元序列。
範例
以下程式碼範例示範如何使用此 GetDecoder 方法取得解碼器,將 UTF-7 編碼位元組轉換為字元序列。
using System;
using System.Text;
class UTF7EncodingExample {
public static void Main() {
Char[] chars;
Byte[] bytes = new Byte[] {
99, 43, 65, 119, 67, 103, 111, 65, 45
};
Decoder utf7Decoder = Encoding.UTF7.GetDecoder();
int charCount = utf7Decoder.GetCharCount(bytes, 0, bytes.Length);
chars = new Char[charCount];
int charsDecodedCount = utf7Decoder.GetChars(bytes, 0, bytes.Length, chars, 0);
Console.WriteLine(
"{0} characters used to decode bytes.", charsDecodedCount
);
Console.Write("Decoded chars: ");
foreach (Char c in chars) {
Console.Write("[{0}]", c);
}
Console.WriteLine();
}
}
Imports System.Text
Class UTF7EncodingExample
Public Shared Sub Main()
Dim chars() As Char
Dim bytes() As Byte = {99, 43, 65, 119, 67, 103, 111, 65, 45}
Dim utf7Decoder As Decoder = Encoding.UTF7.GetDecoder()
Dim charCount As Integer = utf7Decoder.GetCharCount(bytes, 0, bytes.Length)
chars = New Char(charCount - 1) {}
Dim charsDecodedCount As Integer = utf7Decoder.GetChars(bytes, 0, bytes.Length, chars, 0)
Console.WriteLine("{0} characters used to decode bytes.", charsDecodedCount)
Console.Write("Decoded chars: ")
Dim c As Char
For Each c In chars
Console.Write("[{0}]", c)
Next c
Console.WriteLine()
End Sub
End Class
備註
此 Decoder.GetChars 方法將連續的位元組區塊轉換為連續的字元區塊, GetChars 方式類似於此類別的方法。 然而,a Decoder 會維持呼叫間的狀態資訊,以便正確解碼跨區塊的位元組序列。 同時 Decoder 會保留資料區塊末尾的尾部位元組,並在下一次解碼操作中使用這些尾部位元組。 因此, GetDecoder 和 GetEncoder 對於網路傳輸和檔案操作非常有用,因為這些操作通常處理的是資料區塊,而非完整的資料流。