Decoder Class
Converts encoded blocks of bytes into blocks of Unicode characters.
For a list of all members of this type, see Decoder Members.
System.Object
System.Text.Decoder
[Visual Basic] <Serializable> MustInherit Public Class Decoder [C#] [Serializable] public abstract class Decoder [C++] [Serializable] public __gc __abstract class Decoder [JScript] public Serializable abstract class Decoder
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
This is an abstract class. You must implement and override all its methods in a derived class.
An implementation of this class converts blocks of encoded bytes into blocks of Unicode characters through successive calls of the GetChars method. This class maintains state information between successive calls of GetChars, enabling it to decode a sequence of bytes that span adjacent blocks. For example, use GetChars to decode a sequence of bytes that does not have a specific end, such as a stream.
The GetCharCount method calculates the number of characters yielded by decoding a specified block of bytes.
Use the GetDecoder method of classes derived from the System.Text.Encoding class to obtain an instance of the Decoder class.
Example
[Visual Basic, C#, C++] The following code example demonstrates using a Decoder to convert two different byte arrays into a character array. One of the character's bytes spans the arrays. This is similar to what a System.IO.StreamReader does internally when reading a stream.
[Visual Basic] Imports System Imports System.Text Public Class dec Public Shared Sub Main() ' These bytes in UTF-8 correspond to 3 different Unicode ' characters: space (U+0020), # (U+0023), and the biohazard ' symbol (U+2623). Note the biohazard symbol requires 3 bytes ' in UTF-8 (hexadecimal e2, 98, e3). Decoders store state across ' multiple calls to GetChars, handling the case when one char ' is in multiple byte arrays. Dim bytes1 As Byte() = {&H20, &H23, &HE2} Dim bytes2 As Byte() = {&H98, &HE3} Dim chars(3) As Char Dim d As Decoder = Encoding.UTF8.GetDecoder() Dim charLen As Integer = d.GetChars(bytes1, 0, bytes1.Length, chars, 0) ' The value of charLen should be 2 now. charLen += d.GetChars(bytes2, 0, bytes2.Length, chars, charLen) Dim c As Char For Each c In chars Console.Write("U+" + Convert.ToUInt16(c).ToString() + " ") Next c End Sub End Class [C#] using System; using System.Text; public class dec { public static void Main() { // These bytes in UTF-8 correspond to 3 different Unicode // characters: space (U+0020), # (U+0023), and the biohazard // symbol (U+2623). Note the biohazard symbol requires 3 bytes // in UTF-8 (hexadecimal e2, 98, e3). Decoders store state across // multiple calls to GetChars, handling the case when one char // is in multiple byte arrays. byte[] bytes1 = { 0x20, 0x23, 0xe2 }; byte[] bytes2 = { 0x98, 0xe3 }; char[] chars = new char[3]; Decoder d = Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(bytes1, 0, bytes1.Length, chars, 0); // The value of charLen should be 2 now. charLen += d.GetChars(bytes2, 0, bytes2.Length, chars, charLen); foreach(char c in chars) Console.Write("U+" + ((ushort)c).ToString() + " "); } } [C++] using namespace System; using namespace System::Text; int main() { // These bytes in UTF-8 correspond to 3 different Unicode // characters: space (U+0020), # (U+0023), and the biohazard // symbol (U+2623). Note the biohazard symbol requires 3 bytes // in UTF-8 (hexadecimal e2, 98, e3). Decoders store state across // multiple calls to GetChars, handling the case when one char // is in multiple byte arrays. Byte bytes1[] = { 0x20, 0x23, 0xe2 }; Byte bytes2[] = { 0x98, 0xe3 }; Char chars[] = new Char[3]; Decoder* d = Encoding::UTF8->GetDecoder(); int charLen = d->GetChars(bytes1, 0, bytes1->Length, chars, 0); // The value of charLen should be 2 now. charLen += d->GetChars(bytes2, 0, bytes2->Length, chars, charLen); for( UInt16 index(0); index < chars->Length; ++index ) { Console::Write( String::Concat( S"U+", static_cast<UInt16>(chars[index]).ToString(), S" " ) ); } };
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Text
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: Mscorlib (in Mscorlib.dll)