See Also: Encoder Members
The following example demonstrates using the System.Text.UTF8Encoding class to convert one character array to two byte arrays.
C# Example
using System;
using System.Text;
public class EncoderExample
{
public static void Main()
{
string str = "Encoder";
char[] cAry = str.ToCharArray();
UTF8Encoding utf = new UTF8Encoding();
Encoder e = utf.GetEncoder();
int count1 =
e.GetByteCount(cAry,0,cAry.Length-4,false);
int count2 =
e.GetByteCount(cAry,cAry.Length-4,4,true);
byte[] bytes1 = new byte[count1];
byte[] bytes2 = new byte[count2];
e.GetBytes(cAry,0,cAry.Length-4,bytes1,0,false);
e.GetBytes(cAry,cAry.Length-4,4,bytes2,0,true);
Console.Write("Bytes1: ");
foreach (byte b in bytes1)
Console.Write(" '{0}' ", b);
Console.WriteLine();
Console.Write("Bytes2: ");
foreach (byte b in bytes2)
Console.Write(" '{0}' ", b);
Console.WriteLine();
}
}
The output is
Bytes1: '69' '110' '99'