System.Text.Decoder Class

Assembly: Mscorlib.dll
Namespace: System.Text
Summary
Converts encoded blocks of bytes into blocks of Unicode characters.
C# Syntax:
[Serializable]
public abstract class Decoder
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 Decoder.GetChars method. This class maintains state information between successive calls of Decoder.GetChars, enabling it to decode a sequence of bytes that span adjacent blocks. For example, use Decoder.GetChars to decode a sequence of bytes that does not have a specific end, such as a stream.

The Decoder.GetCharCount method calculates the number of characters yielded by decoding a specified block of bytes.

Use the GetDecoder method of classes derived from the Encoding class to obtain an instance of the Decoder class.

Example
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 StreamReader does internally when reading a stream.
 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() + "  ");
     }
 }

    
See also:
System.Text Namespace

System.Text.Decoder Member List:

Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetCharCount When overridden in a derived class, calculates the number of characters Decoder.GetChars would produce from decoding the specified range of bytes.
GetChars When overridden in a derived class, decodes a specified range of elements from a byte array and stores them in a specified range of a Unicode character array.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the Decoder class.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Text.Decoder Member Details

ctor #1
Summary
Initializes a new instance of the Decoder class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
protected Decoder();

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~Decoder();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetCharCount(
   byte[] bytes,
   int index,
   int count
)
Summary
When overridden in a derived class, calculates the number of characters Decoder.GetChars would produce from decoding the specified range of bytes.
C# Syntax:
public abstract int GetCharCount(
   byte[] bytes,
   int index,
   int count
);
Parameters:

bytes

The byte array to decode.

index

The index of the first byte in bytes to decode.

count

The number of bytes to decode.

Return Value:
The number of characters the next call to Decoder.GetChars would produce from decoding the specified range of elements in bytes.
Exceptions
Exception Type Condition
ArgumentNullException bytes is null.
ArgumentOutOfRangeException index or count is less than zero.

-or-

index plus count is greater than the length of bytes.

Remarks
Use Decoder.GetCharCount to calculate the array size required by the Decoder.GetChars method to store the decoded bytes.

The state of the decoder is not affected by calling this method.

Return to top


Method: GetChars(
   byte[] bytes,
   int byteIndex,
   int byteCount,
   char[] chars,
   int charIndex
)
Summary
When overridden in a derived class, decodes a specified range of elements from a byte array and stores them in a specified range of a Unicode character array.
C# Syntax:
public abstract int GetChars(
   byte[] bytes,
   int byteIndex,
   int byteCount,
   char[] chars,
   int charIndex
);
Parameters:

bytes

A byte array to decode.

byteIndex

The index of the first element in bytes to decode.

byteCount

The number of elements to decode.

chars

The character array where the decoded results are stored.

charIndex

The index of the first element in chars to store the decoded results.

Return Value:
The number of characters decoded into chars.
Exceptions
Exception Type Condition
ArgumentNullException bytes or chars is null.
ArgumentOutOfRangeException byteIndex, byteCount, or charIndex is less than zero.

-or-

byteIndex plus byteCount is greater than the length of bytes.

-or-

charIndex is greater than the length of chars.

Remarks
Use Decoder.GetCharCount to calculate the array size required by the Decoder.GetChars method to store decoded bytes.

It is recommended that you always call Decoder.GetCharCount before calling Decoder.GetChars because the Decoder.GetChars method might change the internal state information between blocks of bytes.

See also:
Decoder.GetCharCount | Encoding.GetChars

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.