UnicodeEncoding.GetEncoder Method

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)

'Declaration
<ComVisibleAttribute(False)> _
Public Overrides Function GetEncoder As Encoder

Return Value

Type: System.Text.Encoder
A Encoder 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 GetBytes method of this class. However, a 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 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 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.

Imports System
Imports System.Text
Imports Microsoft.VisualBasic

Public Class SamplesUnicodeEncoding   

   Public Shared Sub Main()

      ' 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(&H0306), ChrW(&H01FD), ChrW(&H03B2)}
      Console.Write("The original characters : ")
      Console.WriteLine(myChars)

      ' 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.
      Console.Write("Using the encoder       : ")
      Dim i As Integer 
      For i = 0 To myBytes.Length - 1
         Console.Write("{0:X2} ", myBytes(i))
      Next i
      Console.WriteLine()

      ' 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.
      Console.Write("Using the decoder       : ")
      Console.WriteLine(myDecodedChars)

   End Sub 'Main 

End Class 'SamplesUnicodeEncoding


'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 61 00 06 03 FD 01 B2 03 
'Using the decoder       : za??ß

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft