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

Description

ASCIIEncoding GetBytes(Char[], Int32, Int32, Byte[], Int32) Encodes a set of characters from the specified character array into the specified byte array.

Syntax

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


public override int GetBytes(
  char[] chars,//ww w  .j a  va  2s.  com
  int charIndex,
  int charCount,
  byte[] bytes,
  int byteIndex
)

Parameters

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

  • chars - The character array containing the set of characters to encode.
  • charIndex - The index of the first character to encode.
  • charCount - The number of characters to encode.
  • bytes - The byte array to contain the resulting sequence of bytes.
  • byteIndex - The index at which to start writing the resulting sequence of bytes.

Returns

ASCIIEncoding.GetBytes(Char[], Int32, Int32, Byte[], Int32) method returns The actual number of bytes written into bytes.

Example


using System;//from  w ww .  j  a v a  2s . co m
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        Byte[] bytes;
        String chars = "ASCII Encoding Example";

        ASCIIEncoding ascii = new ASCIIEncoding();

        int byteCount = ascii.GetByteCount(chars.ToCharArray(), 6, 8);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = ascii.GetBytes(chars, 6, 8, bytes, 0);

        Console.WriteLine(bytesEncodedCount);

        foreach (Byte b in bytes) {
            Console.Write("[{0}]", b);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Text »




ASCIIEncoding
Encoding
EncodingInfo
StringBuilder
UnicodeEncoding
UTF8Encoding