UTF8Encoding.GetDecoder メソッド

定義

UTF-8 でエンコードされたバイト シーケンスを 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

返品

UTF-8 でエンコードされたバイト シーケンスを Unicode 文字のシーケンスに変換するデコーダー。

次の例では、 GetDecoder メソッドを使用して UTF-8 デコーダーを取得します。 デコーダーは、バイトのシーケンスを文字のシーケンスに変換します。

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
            99, 204, 128, 234, 130, 160
        };

        Decoder utf8Decoder = Encoding.UTF8.GetDecoder();

        int charCount = utf8Decoder.GetCharCount(bytes, 0, bytes.Length);
        chars = new Char[charCount];
        int charsDecodedCount = utf8Decoder.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 UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim chars() As Char
        Dim bytes() As Byte = {99, 204, 128, 234, 130, 160}
        
        Dim utf8Decoder As Decoder = Encoding.UTF8.GetDecoder()
        
        Dim charCount As Integer = utf8Decoder.GetCharCount(bytes, 0, bytes.Length)
        chars = New Char(charCount - 1) {}
        Dim charsDecodedCount As Integer = utf8Decoder.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 メソッドと同様の方法で、バイトのシーケンシャル ブロックを文字のシーケンシャル ブロックに変換します。 ただし、 Decoder は、ブロックにまたがるバイト シーケンスを正しくデコードできるように、呼び出し間の状態情報を保持します。 Decoderでは、データ ブロックの末尾にある末尾のバイトも保持され、次のデコード操作で末尾のバイトが使用されます。 そのため、 GetDecoderGetEncoder は、ネットワーク転送とファイル操作に役立ちます。これらの操作は、多くの場合、完全なデータ ストリームではなくデータ ブロックを処理するためです。

エラー検出が有効になっている場合(つまり、コンストラクターの throwOnInvalidCharacters パラメーターが trueに設定されている場合は、このメソッドによって返される Decoder でもエラー検出が有効になります。 エラー検出が有効で、無効なシーケンスが検出された場合、デコーダーの状態は未定義であり、処理を停止する必要があります。

適用対象

こちらもご覧ください