Share via


UTF7Encoding.GetDecoder Método

Definição

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

Exemplos

O exemplo de código a seguir demonstra como usar o GetDecoder método para obter um decodificador para converter os bytes codificados utf-7 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,43,65,119,67,103,111,65,45};
   Decoder^ utf7Decoder = Encoding::UTF7->GetDecoder();
   int charCount = utf7Decoder->GetCharCount( bytes, 0, bytes->Length );
   chars = gcnew array<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: " );
   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 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

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.

Aplica-se a

Confira também