C# Encoding GetBytes(Char[], Int32, Int32, Byte[], Int32)
Description
Encoding GetBytes(Char[], Int32, Int32, Byte[], Int32)
When
overridden in a derived class, encodes a set of characters from the specified
character array into the specified byte array.
Syntax
Encoding.GetBytes(Char[], Int32, Int32, Byte[], Int32)
has the following syntax.
public abstract int GetBytes(
char[] chars,//from w w w . j a v a 2s . c om
int charIndex,
int charCount,
byte[] bytes,
int byteIndex
)
Parameters
Encoding.GetBytes(Char[], Int32, Int32, Byte[], Int32)
has the following parameters.
chars
- The character array containing the set of characters to encode.charIndex
- The index of the first character to encode.charCount
- The number of characters to encode.bytes
- The byte array to contain the resulting sequence of bytes.byteIndex
- The index at which to start writing the resulting sequence of bytes.
Returns
Encoding.GetBytes(Char[], Int32, Int32, Byte[], Int32)
method returns The actual number of bytes written into bytes.
Example
using System;//from w w w.j a v a 2s . com
using System.Text;
public class SamplesEncoding
{
public static void Main()
{
char[] myChars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
Encoding enc = Encoding.UTF8;
byte[] bytes = new byte[100];
enc.GetBytes(myChars, 0, 3, bytes, bytes.GetLowerBound(0));
for (int i = 0; i < bytes.Length; i++)
Console.Write("{0:X2} ", bytes[i]);
Console.WriteLine();
}
}
The code above generates the following result.