C# Encoding GetChars(Byte[], Int32, Int32, Char[], Int32)
Description
Encoding GetChars(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.
Syntax
Encoding.GetChars(Byte[], Int32, Int32, Char[], Int32)
has the following syntax.
public abstract int GetChars(
byte[] bytes,//from w w w . j a v a 2 s. c o m
int byteIndex,
int byteCount,
char[] chars,
int charIndex
)
Parameters
Encoding.GetChars(Byte[], Int32, Int32, Char[], Int32)
has the following 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.
Returns
Encoding.GetChars(Byte[], Int32, Int32, Char[], Int32)
method returns The actual number of characters written into chars.
Example
The following example converts a string from one encoding to another.
//w ww. j a v a 2 s . co m
using System;
using System.Text;
class Example
{
static void Main()
{
string unicodeString = "This string contains the unicode character Pi (\u03a0)";
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
}
}
The code above generates the following result.