C# UTF8Encoding GetBytes(Char[], Int32, Int32, Byte[], Int32)
Description
UTF8Encoding GetBytes(Char[], Int32, Int32, Byte[], Int32)
Encodes a set of characters from the specified character array into
the specified byte array.
Syntax
UTF8Encoding.GetBytes(Char[], Int32, Int32, Byte[], Int32)
has the following syntax.
public override int GetBytes(
char[] chars,/*from w w w . j a v a2 s . c om*/
int charIndex,
int charCount,
byte[] bytes,
int byteIndex
)
Parameters
UTF8Encoding.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
UTF8Encoding.GetBytes(Char[], Int32, Int32, Byte[], Int32)
method returns The actual number of bytes written into bytes.
Example
using System;/*w w w .ja v a 2 s . co m*/
using System.Text;
class UTF8EncodingExample {
public static void Main() {
Byte[] bytes;
String chars = "UTF8 Encoding Example";
UTF8Encoding utf8 = new UTF8Encoding();
int byteCount = utf8.GetByteCount(chars.ToCharArray(), 0, 13);
bytes = new Byte[byteCount];
int bytesEncodedCount = utf8.GetBytes(chars, 0, 13, bytes, 0);
Console.WriteLine(bytesEncodedCount);
foreach (Byte b in bytes) {
Console.WriteLine("[{0}]", b);
}
}
}
The code above generates the following result.