System.Text.Encoding.GetChars Method

When overridden in a derived class, decodes a sequence of bytes from the specified byte array into the specified character array.

Syntax

public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)

Parameters

bytes
The byte array containing the sequence of bytes to decode.
byteIndex
The index of the first byte to decode.
byteCount
The number of bytes to decode.
chars
The character array to contain the resulting set of characters.
charIndex
The index at which to start writing the resulting set of characters.

Returns

The actual number of characters written into chars.

Exceptions

TypeReason
ArgumentException chars does not contain sufficient space to store the decoded characters.
ArgumentNullException

bytes is null .

-or-

chars is null .

ArgumentOutOfRangeException

byteIndex < 0.

-or-

byteCount < 0.

-or-

charIndex < 0.

-or-

byteIndex and byteCount do not specify a valid range in bytes (i.e. (byteIndex + byteCount ) > bytes.Length).

-or-

charIndex > chars.Length.

Remarks

To calculate the exact array size required by Encoding.GetChars(Byte[]) to store the resulting characters, the application should use Encoding.GetCharCount(Byte[]). To calculate the maximum array size, the application should use Encoding.GetMaxCharCount(int). The Encoding.GetCharCount(Byte[]) method generally allows allocation of less memory, while the Encoding.GetMaxCharCount(int) method generally executes faster.

Encoding.GetChars(Byte[], int, int, Char[], int) gets characters from an input byte sequence. Encoding.GetChars(Byte[], int, int, Char[], int) is different than erload:System.Text.Decoder.GetChars because System.Text.Encoding expects discrete conversions, while System.Text.Decoder is designed for multiple passes on a single input stream.

If the data to be converted is available only in sequential blocks (such as data read from a stream) or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the System.Text.Decoder or the System.Text.Encoder provided by the Encoding.GetDecoder method or the Encoding.GetEncoder method, respectively, of a derived class.

Note    This method is intended to operate on Unicode characters, not on arbitrary binary data, such as byte arrays. If your application needs to encode arbitrary binary data into text, it should use a protocol such as uuencode, which is implemented by methods such as Convert.ToBase64CharArray(Byte[], int, int, Char[], int).

The Encoding.GetCharCount(Byte[]) method determines how many characters result in decoding a sequence of bytes, and the Encoding.GetChars(Byte[]) method performs the actual decoding. The Encoding.GetChars(Byte[]) method expects discrete conversions, in contrast to the Decoder.GetChars(Byte[], int, int, Char[], int) method, which handles multiple passes on a single input stream.

Several versions of Encoding.GetCharCount(Byte[]) and Encoding.GetChars(Byte[]) are supported. The following are some programming considerations for use of these methods:

Requirements

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