Mega Code Archive

 
Categories / C# / Internationalization
 

Converts a sequence of Unicode characters into a UTF-8 encoded sequence of bytes

using System; using System.Text; class UTF8EncodingExample {     public static void Main() {         Char[] chars = new Char[] {'\u0300', '\ua0a0'};         Byte[] bytes;         Encoder utf8Encoder = Encoding.UTF8.GetEncoder();         int byteCount = utf8Encoder.GetByteCount(chars, 2, 3, true);         bytes = new Byte[byteCount];         int bytesEncodedCount = utf8Encoder.GetBytes(chars, 2, 3, bytes, 0, true);         Console.WriteLine(bytesEncodedCount);         foreach (Byte b in bytes) {             Console.Write("[{0}]", b);         }     } }