C# UTF8Encoding GetBytes(Char[], Int32, Int32)

Description

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

Syntax

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


public virtual byte[] GetBytes(
  char[] chars,//from   ww w  .  j  a  v  a2 s  .c om
  int index,
  int count
)

Parameters

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

  • chars - The character array containing the set of characters to encode.
  • index - The index of the first character to encode.
  • count - The number of characters to encode.

Returns

UTF8Encoding.GetBytes(Char[], Int32, Int32) method returns

Example


//from w w  w . j  a va 2 s .c  o  m
using System;
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;
      PrintCountsAndBytes( myChars, 4, 3, u8 );
   }
   public static void PrintCountsAndBytes( char[] chars, int index, int count, Encoding enc )  {
      byte[] bytes = enc.GetBytes( chars, index, count );
      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