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

Description

ASCIIEncoding 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

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


public virtual byte[] GetBytes(
  char[] chars,/*from  w  w w.j av  a 2  s  .c om*/
  int index,
  int count
)

Parameters

ASCIIEncoding.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

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

Example


using System;/*  w ww. j  av  a 2s  .  com*/
using System.Text;

public class SamplesEncoding
{

    public static void Main()
    {
        char[] chars = new char[] { 'z', 'a', '\u0306', '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
        ASCIIEncoding enc = new ASCIIEncoding();

        Console.Write("{0,-30} :", enc.ToString());
        int index = 1;
        int count = 3;
        int iBC = enc.GetByteCount(chars, index, count);
        Console.Write(" {0,-3}", iBC);

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

        byte[] bytes = enc.GetBytes(chars, index, count);

        enc.GetBytes(chars, index, count, bytes, bytes.GetLowerBound(0));

        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