UnicodeEncoding.GetDecoder Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
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)
Syntax
'Declaration
Public Overrides Function GetDecoder As Decoder
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.
Remarks
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.
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.
See Also