C# ASCIIEncoding GetBytes(String)

Description

ASCIIEncoding GetBytes(String) When overridden in a derived class, encodes all the characters in the specified string into a sequence of bytes.

Syntax

ASCIIEncoding.GetBytes(String) has the following syntax.


public virtual byte[] GetBytes(
  string s
)

Parameters

ASCIIEncoding.GetBytes(String) has the following parameters.

  • s - The string containing the characters to encode.

Returns

ASCIIEncoding.GetBytes(String) method returns

Example


using System;/*from  ww  w .  ja  v  a2 s  .c o  m*/
using System.Text;

public class SamplesEncoding  {

   public static void Main()  {
      String s = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";
      ASCIIEncoding enc = new ASCIIEncoding();
      
      Console.Write( "{0,-30} :", enc.ToString() );

      int iBC  = enc.GetByteCount( s );
      Console.Write( " {0,-3}", iBC );

      int iMBC = enc.GetMaxByteCount( s.Length );
      Console.Write( " {0,-3} :", iMBC );

      byte[] bytes = enc.GetBytes( s );

      for ( int i = 0; i < bytes.Length; i++ )
            Console.Write( "{0:X2} ", bytes[i] );
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding