Mega Code Archive

 
Categories / C# / Internationalization
 

Encodes a set of characters from the specified character array into the specified byte array

using System; using System.Text; class UTF8EncodingExample {     public static void Main() {         Byte[] bytes;         String chars = "this is a test.This is a test";         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.Write("[{0}]", b);         }     } }