UnicodeEncoding.GetDecoder Method

Obtains a decoder that converts a UTF-16 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-16 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 UnicodeEncoding.GetChars method. However, a Decoder object 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 operations in which a single set of data spans multiple blocks.

If error detection is enabled, that is, the throwOnInvalidBytes parameter of the constructor is 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 code 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 Example

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)

      // Get an encoder and a decoder from UnicodeEncoding.
      UnicodeEncoding u16 = new UnicodeEncoding(false, true, true);
      Encoder myEnc = u16.GetEncoder();
      Decoder myDec = u16.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' };
      outputBlock.Text += "The original characters : ";
      outputBlock.Text += myChars + "\n";

      // 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.
      outputBlock.Text += "Using the encoder       : ";
      for (int i = 0; i < myBytes.Length; i++)
         outputBlock.Text += String.Format("{0:X2} ", myBytes[i]);
      outputBlock.Text += "\n";

      // 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.
      outputBlock.Text += "Using the decoder       : ";
      outputBlock.Text += myDecodedChars + "\n";



This example displays the following output:
      The original characters : zăǽβ
      Using the encoder       : 7A 00 61 00 06 03 FD 01 B2 03 
      Using the decoder       : zăǽβ


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions