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

戻り値

Decoder

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

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

using namespace System;
using namespace System::Text;
using namespace System::Collections;
int main()
{
   array<Char>^chars;
   array<Byte>^bytes = {99,204,128,234,130,160};
   Decoder^ utf8Decoder = Encoding::UTF8->GetDecoder();
   int charCount = utf8Decoder->GetCharCount( bytes, 0, bytes->Length );
   chars = gcnew array<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: " );
   IEnumerator^ myEnum = chars->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Char c = safe_cast<Char>(myEnum->Current);
      Console::Write( "[{0}]", c.ToString() );
   }

   Console::WriteLine();
}
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 ます。 ただし、A は Decoder 呼び出し間で状態情報を保持するため、ブロックにまたがるバイト シーケンスを正しくデコードできます。 また、は、 Decoder データブロックの末尾で末尾のバイトを保持し、次のデコード操作で末尾のバイトを使用します。 そのため、 GetDecoderGetEncoder は、ネットワークの転送およびファイル操作に役立ちます。これらの操作は、多くの場合、完全なデータストリームではなく、データのブロックを処理するためです。

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

適用対象

こちらもご覧ください