C# UTF8Encoding GetBytes(Char[])

Description

UTF8Encoding GetBytes(Char[]) When overridden in a derived class, encodes all the characters in the specified character array into a sequence of bytes.

Syntax

UTF8Encoding.GetBytes(Char[]) has the following syntax.


public virtual byte[] GetBytes(
  char[] chars
)

Parameters

UTF8Encoding.GetBytes(Char[]) has the following parameters.

  • chars - The character array containing the characters to encode.

Returns

UTF8Encoding.GetBytes(Char[]) method returns

Example


using System;//ww w.ja v  a  2  s  . c o  m
using System.Text;

public class SamplesEncoding  {

   public static void Main()  {
      char[] myChars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
      Encoding  u8    = Encoding.UTF8;
      byte[] bytes = u8.GetBytes( myChars );
      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();
      }

   }

}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding