Export (0) Print
Expand All

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

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

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

public abstract int GetChars (
	byte[] bytes,
	int byteIndex,
	int byteCount,
	char[] chars,
	int charIndex
)
public abstract int GetChars (
	byte[] bytes, 
	int byteIndex, 
	int byteCount, 
	char[] chars, 
	int charIndex
)
public abstract 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

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.

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, of a derived class.

The following code example converts a string from one encoding to another.

using System;
using System.Text;

namespace ConvertExample
{
   class ConvertExampleClass
   {
      static void Main()
      {
         string unicodeString = "This string contains the unicode character Pi(\u03a0)";

         // Create two different encodings.
         Encoding ascii = Encoding.ASCII;
         Encoding unicode = Encoding.Unicode;

         // Convert the string into a byte[].
         byte[] unicodeBytes = unicode.GetBytes(unicodeString);

         // Perform the conversion from one encoding to the other.
         byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
            
         // Convert the new byte[] into a char[] and then into a string.
         // This is a slightly different approach to converting to illustrate
         // the use of GetCharCount/GetChars.
         char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
         ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
         string asciiString = new string(asciiChars);

         // Display the strings created before and after the conversion.
         Console.WriteLine("Original string: {0}", unicodeString);
         Console.WriteLine("Ascii converted string: {0}", asciiString);
      }
   }
}

package ConvertExample; 

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

class ConvertExampleClass
{
    public static void main(String[] args)
    {
        String unicodeString = 
                "This string contains the unicode character Pi(\u03a0)";

        // Create two different encodings.
        Encoding ascii = Encoding.get_ASCII();
        Encoding unicode = Encoding.get_Unicode();

        // Convert the string into a byte[].
        ubyte unicodeBytes[] = unicode.GetBytes(unicodeString);

        // Perform the conversion from one encoding to the other.
        ubyte asciiBytes[] = Encoding.Convert(unicode, ascii, unicodeBytes);

        // Convert the new byte[] into a char[] and then into a string.
        // This is a slightly different approach to converting to illustrate
        // the use of GetCharCount/GetChars.
        char asciiChars[] = new 
                char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.length)];
        ascii.GetChars(asciiBytes, 0, asciiBytes.length, asciiChars, 0);
        String asciiString = new String(asciiChars);

        // Display the strings created before and after the conversion.
        Console.WriteLine("Original string: {0}", unicodeString);
        Console.WriteLine("Ascii converted string: {0}", asciiString);
    } //main
} //ConvertExampleClass 

The following code example encodes a string into an array of bytes, and then decodes a range of the bytes into an array of characters.

using System;
using System.Text;

public class SamplesEncoding  {

   public static void Main()  {

      // Create two instances of UTF32Encoding: one with little-endian byte order and one with big-endian byte order.
      Encoding u32LE = Encoding.GetEncoding( "utf-32" );
      Encoding u32BE = Encoding.GetEncoding( "utf-32BE" );

      // Use a string containing the following characters:
      //    Latin Small Letter Z (U+007A)
      //    Latin Small Letter A (U+0061)
      //    Combining Breve (U+0306)
      //    Latin Small Letter AE With Acute (U+01FD)
      //    Greek Small Letter Beta (U+03B2)
      String myStr = "za\u0306\u01FD\u03B2";

      // Encode the string using the big-endian byte order.
      byte[] barrBE = new byte[u32BE.GetByteCount( myStr )];
      u32BE.GetBytes( myStr, 0, myStr.Length, barrBE, 0 );

      // Encode the string using the little-endian byte order.
      byte[] barrLE = new byte[u32LE.GetByteCount( myStr )];
      u32LE.GetBytes( myStr, 0, myStr.Length, barrLE, 0 );

      // Get the char counts, decode eight bytes starting at index 0,
      // and print out the counts and the resulting bytes.
      Console.Write( "BE array with BE encoding : " );
      PrintCountsAndChars( barrBE, 0, 8, u32BE );
      Console.Write( "LE array with LE encoding : " );
      PrintCountsAndChars( barrLE, 0, 8, u32LE );

   }


   public static void PrintCountsAndChars( byte[] bytes, int index, int count, Encoding enc )  {

      // Display the name of the encoding used.
      Console.Write( "{0,-25} :", enc.ToString() );

      // Display the exact character count.
      int iCC  = enc.GetCharCount( bytes, index, count );
      Console.Write( " {0,-3}", iCC );

      // Display the maximum character count.
      int iMCC = enc.GetMaxCharCount( count );
      Console.Write( " {0,-3} :", iMCC );

      // Decode the bytes and display the characters.
      char[] chars = enc.GetChars( bytes, index, count );

      // The following is an alternative way to decode the bytes:
      // char[] chars = new char[iCC];
      // enc.GetChars( bytes, index, count, chars, 0 );

      Console.WriteLine( chars );

   }

}


/* 
This code produces the following output.  The question marks take the place of characters that cannot be displayed at the console.

BE array with BE encoding : System.Text.UTF32Encoding : 2   6   :za
LE array with LE encoding : System.Text.UTF32Encoding : 2   6   :za

*/


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

public class SamplesEncoding
{
    public static void main(String[] args)
    {
        // Create two instances of UTF32Encoding: one with little-endian 
        // byte order and one with big-endian byte order.
        Encoding u32LE = Encoding.GetEncoding("utf-32");
        Encoding u32BE = Encoding.GetEncoding("utf-32BE");

        // Use a string containing the following characters:
        //    Latin Small Letter Z (U+007A)
        //    Latin Small Letter A (U+0061)
        //    Combining Breve (U+0306)
        //    Latin Small Letter AE With Acute (U+01FD)
        //    Greek Small Letter Beta (U+03B2)
        String myStr = "za\u0306\u01FD\u03B2";

        // Encode the string using the big-endian byte order.
        ubyte barrBE[] = new ubyte[u32BE.GetByteCount(myStr)];
        u32BE.GetBytes(myStr, 0, myStr.get_Length(), barrBE, 0);

        // Encode the string using the little-endian byte order.
        ubyte barrLE[] = new ubyte[u32LE.GetByteCount(myStr)];
        u32LE.GetBytes(myStr, 0, myStr.get_Length(), barrLE, 0);

        // Get the char counts, decode eight bytes starting at index 0,
        // and print out the counts and the resulting bytes.
        Console.Write("BE array with BE encoding : ");
        PrintCountsAndChars(barrBE, 0, 8, u32BE);
        Console.Write("LE array with LE encoding : ");
        PrintCountsAndChars(barrLE, 0, 8, u32LE);
    } //main

    public static void PrintCountsAndChars(ubyte bytes[], 
                    int index, int count, Encoding enc)
    {
        // Display the name of the encoding used.
        Console.Write("{0,-25} :", enc.ToString());

        // Display the exact character count.
        int iCC = enc.GetCharCount(bytes, index, count);
        Console.Write(" {0,-3}", String.valueOf(iCC));

        // Display the maximum character count.
        int iMCC = enc.GetMaxCharCount(count);
        Console.Write(" {0,-3} :", String.valueOf(iMCC));

        // Decode the bytes and display the characters.
        char chars[] = enc.GetChars(bytes, index, count);

        // The following is an alternative way to decode the bytes:
        // char[] chars = new char[iCC];
        // enc.GetChars( bytes, index, count, chars, 0 );
        Console.WriteLine(chars);
    } //PrintCountsAndChars
} //SamplesEncoding

/* 
This code produces the following output.  The question marks take the
 place of characters that cannot be displayed at the console.

BE array with BE encoding : System.Text.UTF32Encoding : 2   6   :za
LE array with LE encoding : System.Text.UTF32Encoding : 2   6   :za

*/


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:
© 2014 Microsoft