UnicodeEncoding.GetEncoder Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
<ComVisibleAttribute(False)> _
Public Overrides Function GetEncoder As Encoder
[ComVisibleAttribute(false)]
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.

Remarks

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.

Examples

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.

Imports System.Text

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Get an encoder and a decoder from UnicodeEncoding.
      Dim u16 As New UnicodeEncoding(False, True, True)
      Dim myEnc As Encoder = u16.GetEncoder()
      Dim myDec As Decoder = 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)
      Dim myChars() As Char = {"z"c, "a"c, ChrW(&H306), ChrW(&H1FD), ChrW(&H3B2)}
      outputBlock.Text &= "The original characters : "
      outputBlock.Text &= myChars & vbCrLf

      ' Encode the character array.
      Dim iBC As Integer = myEnc.GetByteCount(myChars, 0, myChars.Length, True)
      ' NOTE: In Visual Basic, arrays contain one extra element by default.
      '       The following line creates an array with the exact number of elements required.
      Dim myBytes(iBC - 1) As Byte
      myEnc.GetBytes(myChars, 0, myChars.Length, myBytes, 0, True)

      ' Print the resulting bytes.
      outputBlock.Text &= "Using the encoder       : "
      Dim i As Integer
      For i = 0 To myBytes.Length - 1
         outputBlock.Text += String.Format("{0:X2} ", myBytes(i))
      Next i
      outputBlock.Text &= vbCrLf

      ' Decode the byte array back into an array of characters.
      Dim iCC As Integer = myDec.GetCharCount(myBytes, 0, myBytes.Length, True)
      ' NOTE: In Visual Basic, arrays contain one extra element by default.
      '       The following line creates an array with the exact number of elements required.
      Dim myDecodedChars(iCC - 1) As Char
      myDec.GetChars(myBytes, 0, myBytes.Length, myDecodedChars, 0, True)

      ' Print the resulting characters.
      outputBlock.Text &= "Using the decoder       : "
      outputBlock.Text &= myDecodedChars & vbCrLf

   End Sub 'Main 

End Class 'SamplesUnicodeEncoding


' 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ăǽβ
'
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ăǽβ
*/

Version Information

Silverlight

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

Platforms

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