UTF7Encoding.GetDecoder Metoda

Definicja

Uzyskuje dekoder, który konwertuje kodowaną sekwencję bajtów UTF-7 na sekwencję znaków 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

Zwraca

Decoder

Element Decoder , który konwertuje kodowaną sekwencję bajtów UTF-7 na sekwencję znaków Unicode.

Przykłady

Poniższy przykład kodu przedstawia sposób użycia GetDecoder metody w celu uzyskania dekodera w celu przekonwertowania bajtów zakodowanych w formacie UTF-7 na sekwencję znaków.

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

Uwagi

Metoda Decoder.GetChars konwertuje sekwencyjne bloki bajtów na sekwencyjne bloki znaków w sposób podobny do GetChars metody tej klasy. Jednak element Decoder zachowuje informacje o stanie między wywołaniami, dzięki czemu może poprawnie dekodować sekwencje bajtów, które obejmują bloki. Obiekt Decoder zachowuje również końcowe bajty na końcu bloków danych i używa końcowych bajtów w następnej operacji dekodowania. W związku z tym i GetEncoder są przydatne w przypadku operacji transmisji i plików w sieci, GetDecoder ponieważ te operacje często zajmują się blokami danych zamiast pełnego strumienia danych.

Dotyczy

Zobacz też