Get byte count and max byte count : Encoding « Internationalization I18N « C# / C Sharp






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();
      }
   }
}

   
    
    
  








Related examples in the same category

1.Encoding Class represents a character encoding.
2.Encoding.ASCII encoding for the ASCII (7-bit) character set.
3.Gets an encoding for the UTF-16 format that uses the big endian byte order.
4.Read a text file saved with Big Endian Unicode encoding
5.Display only the encodings that have one or more different names
6.Returns an array that contains all encodings.
7.Returns a sequence of bytes that specifies the encoding used.
8.Gets an encoding for the UTF-32 format using the little endian byte order.
9.Gets an encoding for the UTF-8 format.
10.Gets the Windows operating system code page that most closely corresponds to the current encoding.
11.Petscii Decoder
12.Petscii Encoder
13.extends System.Text.Encoding to create Petscii Encoding
14.SignedLoWord from IntPtr
15.SignedHiWord from IntPtr
16.Pad data to a WORD.
17.Returns the high WORD from a DWORD value.
18.Signed Lo Word
19.Signed Hi Word
20.Flip Endian
21.A Big-endian binary writer.
22.Big-endian binary reader
23.EBCDIC To ASCII
24.Is Chinese Character