This topic has not yet been rated - Rate this topic

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

April 12, 2014

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
)

Parameters

bytes
Type: System.Byte []
The byte array containing the sequence of bytes to decode.
byteIndex
Type: System.Int32
The zero-based 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 zero-based 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

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

DecoderFallbackException

A fallback occurred.

To calculate the exact array size required by GetChars to store the resulting characters, the application should use 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.

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 Decoder or the Encoder provided by the GetDecoder method or the GetEncoder method, respectively, of a derived class.

For a discussion of programming considerations for use of this method, see the Encoding class description.

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


using System;
using System.Text;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string unicodeString = "This string contains the unicode character Pi (\u03a0)";

      // Create two different encodings.
      Encoding utf8 = Encoding.UTF8;
      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[] utf8Bytes = Encoding.Convert(unicode, utf8, unicodeBytes);

      // Convert the new byte[] into a char[] and then into a string.
      char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length)];
      utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0);
      string utf8String = new string(utf8Chars);

      // Display the strings created before and after the conversion.
      outputBlock.Text += String.Format("Original string: {0}", unicodeString) + "\n";
      outputBlock.Text += String.Format("Ascii converted string: {0}", utf8String) + "\n";
   }
}
// The example displays the following output:
//    Original string: This string contains the unicode character Pi (Π)
//    Ascii converted string: This string contains the unicode character Pi (?)


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 Example
{

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // 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.
      outputBlock.Text += "BE array with BE encoding : ";
      PrintCountsAndChars(outputBlock, barrBE, 0, 8, u32BE);
      outputBlock.Text += "LE array with LE encoding : ";
      PrintCountsAndChars(outputBlock, barrLE, 0, 8, u32LE);

   }


   public static void PrintCountsAndChars(System.Windows.Controls.TextBlock outputBlock, byte[] bytes, int index, int count, Encoding enc)
   {

      // Display the name of the encoding used.
      outputBlock.Text += String.Format("{0,-25} :", enc.ToString());

      // Display the exact character count.
      int iCC = enc.GetCharCount(bytes, index, count);
      outputBlock.Text += String.Format(" {0,-3}", iCC);

      // Display the maximum character count.
      int iMCC = enc.GetMaxCharCount(count);
      outputBlock.Text += String.Format(" {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 );

      outputBlock.Text += chars + "\n";

   }

}


/* 
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 Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.