Mega Code Archive

 
Categories / C# / Internationalization
 

Obtains a decoder that converts a UTF-16 encoded sequence of bytes into a sequence of Unicode characters

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