UTF8Encoding.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
Assemblies:   System.Text.Encoding.Extensions (in System.Text.Encoding.Extensions.dll)
  mscorlib (in mscorlib.dll)

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

Parameters

bytes
Type: System.Byte[]

The byte array containing the sequence of bytes to decode.

byteIndex
Type: System.Int32

The index of the first byte to decode.

byteCount
Type: System.Int32

The number of bytes to decode.

chars
Type: System.Char[]

The character array to contain the resulting set of characters.

charIndex
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.

ExceptionCondition
ArgumentNullException

bytes is null.

-or-

chars is null.

ArgumentOutOfRangeException

byteIndex or byteCount or charIndex is less than zero.

-or-

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

-or-

charIndex is not a valid index in chars.

ArgumentException

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

-or-

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

DecoderFallbackException

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

-and-

DecoderFallback is set to DecoderExceptionFallback.

To calculate the exact array size required by GetChars to store the resulting characters, call the GetCharCount method. To calculate the maximum array size, call the GetMaxCharCount method. 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 an ArgumentException exception. 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, use the Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively.

The following example uses 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 UTF8EncodingExample {
    public static void Main() {
        Char[] chars;
        Byte[] bytes = new Byte[] {
             85,  84,  70,  56,  32,  69, 110,
             99, 111, 100, 105, 110, 103,  32,
             69, 120,  97, 109, 112, 108, 101
        };

        UTF8Encoding utf8 = new UTF8Encoding();

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

        Console.WriteLine(
            "{0} characters used to decode bytes.", charsDecodedCount
        );

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft