Mega Code Archive

 
Categories / C# Tutorial / Internationalization
 

Use UnicodeEncoding to encode and decode char array

using System; using System.Text; public class MainClass{    public static void Main()  {       UnicodeEncoding u16 = new UnicodeEncoding( false, true, true );       Encoder myEncoder = u16.GetEncoder();       Decoder myDecoder = u16.GetDecoder();       char[] myChars = new char[5] { 'z', 'a', '\u0306', '\u01FD', '\u03B2' };       Console.WriteLine( myChars );       int iBC  = myEncoder.GetByteCount( myChars, 0, myChars.Length, true );       byte[] myBytes = new byte[iBC];       myEncoder.GetBytes( myChars, 0, myChars.Length, myBytes, 0, true );       for ( int i = 0; i < myBytes.Length; i++ ){          Console.WriteLine( "{0:X2} ", myBytes[i] );       }          int iCC  = myDecoder.GetCharCount( myBytes, 0, myBytes.Length, true );       char[] myDecodedChars = new char[iCC];       myDecoder.GetChars( myBytes, 0, myBytes.Length, myDecodedChars, 0, true );       Console.WriteLine( myDecodedChars );    } }