GetChars Method (Byte[], Int32, Int32, Char[], Int32)

UnicodeEncoding.GetChars Method (Byte[], Int32, Int32, Char[], Int32)


Decodes a sequence of bytes from the specified byte array into the specified character array.

Namespace:   System.Text
Assembly:  mscorlib (in mscorlib.dll)

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


Type: System.Byte[]

The byte array containing the sequence of bytes to decode.

Type: System.Int32

The index of the first byte to decode.

Type: System.Int32

The number of bytes to decode.

Type: System.Char[]

The character array to contain the resulting set of characters.

Type: System.Int32

The index at which to start writing the resulting set of characters.

Return Value

Type: System.Int32

The actual number of characters written into chars.

Exception Condition

bytes is null (Nothing).


chars is null (Nothing).


byteIndex or byteCount or charIndex is less than zero.


byteindex and byteCount do not denote a valid range in bytes.


charIndex is not a valid index in chars.


Error detection is enabled, and bytes contains an invalid sequence of bytes.


chars does not have enough capacity from charIndex to the end of the array to accommodate the resulting characters.


A fallback occurred (see Character Encoding in the .NET Framework for fuller explanation)


DecoderFallback is set to DecoderExceptionFallback.

To calculate the exact array size required by GetChars to store the resulting characters, the application uses GetCharCount. To calculate the maximum array size, the application should use GetMaxCharCount. The GetCharCount method generally allocates less memory, while the GetMaxCharCount method generally executes faster.

With error detection, an invalid sequence causes this method to throw a ArgumentException. Without error detection, invalid sequences are ignored, and no exception is thrown.

If the range of bytes to be decoded includes the byte order mark (BOM) and the byte array was returned by a method of a non-BOM aware type, the character U+FFFE is included in the character array returned by this method. You can remove it by calling the String.TrimStart method.

Data to be converted, such as data read from a stream, might be available only in sequential blocks. In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively.

The following example demonstrates how to use the GetChars method to decode a range of elements in a byte array and store the result in a character array.

using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
            85, 0, 110, 0, 105, 0, 99, 0, 111, 0, 100, 0, 101, 0

        UnicodeEncoding Unicode = new UnicodeEncoding();

        int charCount = Unicode.GetCharCount(bytes, 2, 8);
        chars = new Char[charCount];
        int charsDecodedCount = Unicode.GetChars(bytes, 2, 8, chars, 0);

            "{0} characters used to decode bytes.", charsDecodedCount

        Console.Write("Decoded chars: ");
        foreach (Char c in chars) {
            Console.Write("[{0}]", c);

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft