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
Assembly: mscorlib (in mscorlib.dll)

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

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.

Return Value

The actual number of characters written into chars.

Exception typeCondition

ArgumentNullException

bytes is a null reference (Nothing in Visual Basic).

-or-

chars is a null reference (Nothing in Visual Basic).

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.

To calculate the exact array size required by GetChars to store the resulting characters, use GetCharCount. To calculate the maximum array size, use GetMaxCharCount. The GetCharCount method generally allows you to allocate less memory, whereas the GetMaxCharCount method generally executes faster.

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

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, 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 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();
    }
}

import System.*;
import System.Text.*;

class UTF8EncodingExample
{
    public static void main(String[] args)
    {
        char chars[];
        ubyte bytes[] = new ubyte[] { 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.", 
            String.valueOf(charsDecodedCount));
        Console.Write("Decoded chars: ");
        for (int iCtr = 0; iCtr < chars.length; iCtr++) {
            char c = chars[iCtr];
            Console.Write("[{0}]", String.valueOf(c));
        }
        Console.WriteLine();
    } //main
} //UTF8EncodingExample

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show: