UTF7Encoding.GetDecoder 方法

定义

获取可以将 UTF-7 编码的字节序列转换为 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

返回

Decoder

一个 Decoder,用于将 UTF-7 编码的字节序列转换为 Unicode 字符序列。

示例

下面的代码示例演示如何使用 GetDecoder 该方法获取解码器将 UTF-7 编码的字节转换为字符序列。

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

注解

Decoder.GetChars方法使用类似于此类的方法的方式将连续字节块转换为连续的字符块 GetChars 。 但是,在 Decoder 调用之间维护状态信息,以便它可以正确解码跨块的字节序列。 Decoder还保留数据块末尾的尾随字节,并在下一个解码操作中使用尾随字节。 因此, GetDecoderGetEncoder 对于网络传输和文件操作非常有用,因为这些操作经常处理数据块而不是完整的数据流。

适用于

另请参阅