Mega Code Archive

 
Categories / C# / Internationalization
 

Get byte count and max byte count

using System; using System.Text; public class SamplesEncoding  {    public static void Main()  {       char[] myChars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };       Encoding  u7    = Encoding.UTF7;       PrintCountsAndBytes( myChars, u7 );    }    public static void PrintCountsAndBytes( char[] chars, Encoding enc )  {       Console.Write(enc.ToString() );       Console.Write(enc.GetByteCount( chars ));       Console.Write(enc.GetMaxByteCount( chars.Length ));       byte[] bytes = enc.GetBytes( chars );       PrintHexBytes( bytes );    }    public static void PrintHexBytes( byte[] bytes )  {       if (( bytes == null ) || ( bytes.Length == 0 ))          Console.WriteLine( "<none>" );       else  {          for ( int i = 0; i < bytes.Length; i++ )             Console.Write( "{0:X2} ", bytes[i] );          Console.WriteLine();       }    } }