UnicodeEncoding.GetEncoder Method

July 28, 2014

Obtains an encoder that converts a sequence of Unicode characters into a UTF-16 encoded sequence of bytes.

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

public override Encoder GetEncoder()

Return Value

Type: System.Text.Encoder
An object that converts a sequence of Unicode characters into a UTF-16 encoded sequence of bytes.

The Encoder.GetBytes method converts sequential blocks of characters into sequential blocks of bytes in a manner similar to the UnicodeEncoding.GetBytes method. However, an Encoder object maintains state information between calls so that it can correctly encode character sequences that span blocks. The Encoder object also preserves trailing characters at the end of data blocks and uses the trailing characters in the next encoding operation. For example, a data block might end with an unmatched high surrogate, and the matching low surrogate might be in the next data block. 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 Encoder object returned by this method. If error detection is enabled and an invalid sequence is encountered, the state of the encoder 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ăǽβ
*/


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft