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

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

Örnekler

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

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 Decoder yayılan bayt dizilerinin kodunu doğru bir şekilde çözebilmesi için çağrılar 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 GetDecoderGetEncoder 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, yani oluşturucunun throwOnInvalidCharacters 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 durması gerekir.

Şunlara uygulanır

Ayrıca bkz.