Export (0) Print
Expand All

UnicodeEncoding.GetDecoder Method

Obtains a decoder that can convert a UTF-16 encoded sequence of bytes into a sequence of characters.

[Visual Basic]
Overrides Public Function GetDecoder() As Decoder
[C#]
public override Decoder GetDecoder();
[C++]
public: Decoder* GetDecoder();
[JScript]
public override function GetDecoder() : Decoder;

Return Value

A Decoder.

Remarks

The Decoder.GetChars method converts adjacent blocks of bytes into adjacent blocks of characters, in a manner similar to the GetChars method. However, a Decoder maintains state information between calls so it can correctly decode byte sequences that span blocks.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the GetDecoder method to obtain a Unicode decoder. The decoder is used to convert a sequence of encoded bytes in bytes into a sequence of characters in chars.

[Visual Basic] 
Imports System
Imports System.Text

Class UnicodeEncodingExample
    
    Public Shared Sub Main()
        Dim chars() As Char
        Dim bytes() As Byte = {85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0}
        
        Dim unicodeDecoder As Decoder = Encoding.Unicode.GetDecoder()
        
        Dim charCount As Integer = unicodeDecoder.GetCharCount(bytes, 2, 8)
        chars = New Char(charCount - 1) {}
        Dim charsDecodedCount As Integer = unicodeDecoder.GetChars(bytes, 2, 8, 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 'Main
End Class 'UnicodeEncodingExample

[C#] 
using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
            85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0
        };

        Decoder unicodeDecoder = Encoding.Unicode.GetDecoder();

        int charCount = unicodeDecoder.GetCharCount(bytes, 2, 8);
        chars = new Char[charCount];
        int charsDecodedCount = unicodeDecoder.GetChars(bytes, 2, 8, 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();
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;
using namespace System::Collections;

int main()
{
   Char chars[];
   Byte bytes[] =
   {
      85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0
   };

   Decoder * unicodeDecoder = Encoding::Unicode -> GetDecoder();

   int charCount = unicodeDecoder -> GetCharCount(bytes, 2, 8);
   chars = new Char[charCount];
   int charsDecodedCount = unicodeDecoder -> GetChars(bytes, 2, 8, chars, 0);

   Console::WriteLine(S"{0} characters used to decode bytes.", __box(charsDecodedCount));

   Console::Write(S"Decoded chars: ");
   IEnumerator* myEnum = chars->GetEnumerator();
   while (myEnum->MoveNext())
   {
      Char* c = __try_cast<Char*>(myEnum->Current);
      Console::Write(S"[{0}]", c -> ToString());
   }
   Console::WriteLine();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

UnicodeEncoding Class | UnicodeEncoding Members | System.Text Namespace | GetCharCount | GetChars

Show:
© 2014 Microsoft