UTF8Encoding.GetDecoder Método

Definição

Obtém um decodificador que converte uma sequência de bytes codificados em UTF-8 em uma sequência de caracteres 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

Retornos

Decoder

Um decodificador que converte uma sequência de bytes codificados em UTF-8 em uma sequência de caracteres Unicode.

Exemplos

O exemplo a seguir usa o GetDecoder método para obter um decodificador UTF-8. O decodificador converte uma sequência de bytes em uma sequência de caracteres.

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

Comentários

O Decoder.GetChars método converte blocos sequenciais de bytes em blocos sequenciais de caracteres, de maneira semelhante ao GetChars método dessa classe. No entanto, um Decoder mantém informações de estado entre chamadas para que possa decodificar corretamente sequências de bytes que abrangem blocos. O Decoder também preserva os bytes à direita no final dos blocos de dados e usa os bytes à direita na próxima operação de decodificação. Portanto, GetDecoder e GetEncoder são úteis para a transmissão de rede e operações de arquivo, pois essas operações geralmente lidam com blocos de dados em vez de um fluxo de dados completo.

Se a detecção de erro estiver habilitada, ou seja, o throwOnInvalidCharacters parâmetro do construtor será definido como, a truedetecção de erros também será habilitada no Decoder retornado por esse método. Se a detecção de erro estiver habilitada e uma sequência inválida for encontrada, o estado do decodificador será indefinido e o processamento deverá ser interrompido.

Aplica-se a

Confira também