UTF8Encoding.GetDecoder Yöntem

Tanım

UTF-8 kodlanmış bayt dizisini Unicode karakter dizisine dönüştüren bir kod çözücü alır.

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

Döndürülenler

Decoder

UTF-8 kodlanmış bayt dizisini Unicode karakter dizisine dönüştüren kod çözücü.

Örnekler

Aşağıdaki örnekte utf-8 kod çözücü elde etmek için yöntemi kullanılır GetDecoder . Kod çözücü, bayt dizisini bir karakter dizisine dönüştürür.

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

Açıklamalar

yöntemi, Decoder.GetChars baytların sıralı bloklarını bu sınıfın yöntemine benzer şekilde sıralı karakter bloklarına GetChars dönüştürür. Ancak, bloklara yayılan bayt dizilerinin kodunu doğru bir şekilde çözebilmesi için çağrılar Decoder arasında durum bilgilerini korur. ayrıca Decoder veri bloklarının sonundaki sondaki baytları korur ve sonraki kod çözme işleminde sondaki baytları kullanır. Bu nedenle ve GetDecoder GetEncoder ağ iletimi ve dosya işlemleri için yararlıdır çünkü bu işlemler genellikle tam bir veri akışı yerine veri bloklarıyla ilgilenir.

Hata algılama etkinleştirilirse, throwOnInvalidCharacters yani oluşturucunun parametresi olarak ayarlanır true, hata algılama da bu yöntem tarafından döndürülen içinde Decoder etkinleştirilir. Hata algılama etkinleştirilirse ve geçersiz bir diziyle karşılaşılırsa, kod çözücü durumunun tanımsız olması ve işlemenin durdurulması gerekir.

Şunlara uygulanır

Ayrıca bkz.