UTF8Encoding.GetDecoder 메서드

정의

UTF-8로 인코딩된 바이트 시퀀스를 유니코드 문자 시퀀스로 변환하는 디코더를 가져옵니다.

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로 인코딩된 바이트 시퀀스를 유니코드 문자 시퀀스로 변환하는 디코더입니다.

예제

다음 예제에서는 메서드를 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 이 클래스의 메서드와 비슷한 방식으로 순차 바이트 블록을 문자의 순차 블록으로 변환 합니다. 그러나 Decoder 호출 간에 상태 정보를 유지 관리하므로 블록에 걸쳐 있는 바이트 시퀀스를 올바르게 디코딩할 수 있습니다. Decoder또한는 데이터 블록 끝에 후행 바이트를 보존 하 고 다음 디코딩 작업에서 후행 바이트를 사용 합니다. 따라서 GetDecoderGetEncoder 는 네트워크 전송 및 파일 작업에 유용 합니다. 이러한 작업은 대개 전체 데이터 스트림 대신 데이터 블록을 처리 하기 때문입니다.

오류 검색을 사용하도록 설정 throwOnInvalidCharacters 하면 생성자의 매개 변수가 설정되고 이 메서드에서 반환된 오류 Decoder 검색도 사용하도록 설정true됩니다. 오류 검색을 사용하도록 설정하고 잘못된 시퀀스를 발견하면 디코더의 상태가 정의되지 않고 처리가 중지되어야 합니다.

적용 대상

추가 정보