UTF32Encoding.GetDecoder Method ()


Obtains a decoder that converts a UTF-32 encoded sequence of bytes into a sequence of Unicode characters.

Namespace:   System.Text
Assembly:  mscorlib (in mscorlib.dll)

public override Decoder GetDecoder()

Return Value

Type: System.Text.Decoder

A Decoder that converts a UTF-32 encoded sequence of bytes into a sequence of Unicode characters.

The Decoder.GetChars method converts sequential blocks of bytes into sequential 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. The Decoder also preserves trailing bytes at the end of data blocks and uses the trailing bytes in the next decoding operation. Therefore, GetDecoder and GetEncoder are useful for network transmission and file operations, because those operations often deal with blocks of data instead of a complete data stream.

If error detection is enabled, that is, the throwOnInvalidCharacters parameter of the constructor was set to true, error detection is also enabled in the Decoder returned by this method. If error detection is enabled and an invalid sequence is encountered, the state of the decoder is undefined and processing must stop.

The following example uses an encoder and a decoder to encode a string into an array of bytes, and then decode the bytes into an array of characters.

using System;
using System.Text;

public class SamplesUTF32Encoding  {

   public static void Main()  {

      // Get an encoder and a decoder from UTF32Encoding.
      UTF32Encoding u32 = new UTF32Encoding( false, true, true );
      Encoder myEnc = u32.GetEncoder();
      Decoder myDec = u32.GetDecoder();

      // The characters to encode:
      //    Latin Small Letter Z (U+007A)
      //    Latin Small Letter A (U+0061)
      //    Combining Breve (U+0306)
      //    Latin Small Letter AE With Acute (U+01FD)
      //    Greek Small Letter Beta (U+03B2)
      char[] myChars = new char[5] { 'z', 'a', '\u0306', '\u01FD', '\u03B2' };
      Console.Write( "The original characters : " );
      Console.WriteLine( myChars );

      // Encode the character array.
      int iBC  = myEnc.GetByteCount( myChars, 0, myChars.Length, true );
      byte[] myBytes = new byte[iBC];
      myEnc.GetBytes( myChars, 0, myChars.Length, myBytes, 0, true );

      // Print the resulting bytes.
      Console.Write( "Using the encoder       : " );
      for ( int i = 0; i < myBytes.Length; i++ )
         Console.Write( "{0:X2} ", myBytes[i] );

      // Decode the byte array back into an array of characters.
      int iCC  = myDec.GetCharCount( myBytes, 0, myBytes.Length, true );
      char[] myDecodedChars = new char[iCC];
      myDec.GetChars( myBytes, 0, myBytes.Length, myDecodedChars, 0, true );

      // Print the resulting characters.
      Console.Write( "Using the decoder       : " );
      Console.WriteLine( myDecodedChars );



This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.

The original characters : za??�
Using the encoder       : 7A 00 00 00 61 00 00 00 06 03 00 00 FD 01 00 00 B2 03 00 00
Using the decoder       : za??�


Universal Windows Platform
Available since 10
.NET Framework
Available since 2.0
Return to top