System.Text.Decoder Class
Converts blocks of bytes into blocks of characters, maintaining state across successive calls for reading from a System.IO.Stream.

See Also: Decoder Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Decoder

Remarks

Note: Following instantiation of a decoder, sequential blocks of bytes are converted into blocks of characters through calls to the Decoder.GetChars(Byte[], int, int, Char[], int) method. The decoder maintains state between the conversions, allowing it to correctly decode a character whose bytes span multiple blocks. This greatly assists decoding streams of bytes into characters. An instance of a specific implementation of the System.Text.Decoder class is typically obtained through a call to the Encoding.GetDecoder method of a System.Text.Encoding object.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Example

The following example demonstrates using the System.Text.UTF8Encoding implementation of the System.Text.Decoder class to convert two byte arrays to a character array, where one character's bytes span multiple byte arrays. This demonstrates how to use a System.Text.Decoder in streaming-like situations.

C# Example

using System;
using System.Text;

public class DecoderExample 
{
   public static void Main() 
   {
      // These bytes in UTF-8 correspond to 3 different
      // Unicode characters - A (U+0041), # (U+0023),
      // and the biohazard symbol (U+2623). Note the 
      // biohazard symbol requires 3 bytes in UTF-8 
      // (in hex, e2, 98, a3). Decoders store state across
      // multiple calls to GetChars, handling the case 
      // when one char spans multiple byte arrays.

      byte[] bytes1 = { 0x41, 0x23, 0xe2 };
      byte[] bytes2 = { 0x98, 0xa3 };
      char[] chars = new char[3]; 

      Decoder d = Encoding.UTF8.GetDecoder();
      int charLen = d.GetChars(bytes1, 0, bytes1.Length,
                               chars, 0);
      // charLen is 2.

      charLen += d.GetChars(bytes2, 0, bytes2.Length,
                            chars, charLen);
      // charLen is now 3. 

      foreach(char c in chars) 
         Console.Write("U+{0:x} ", (ushort)c);
   }
}

The output is

U+41 U+23 U+2623

Requirements

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0