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

Description

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

Syntax

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


public override int GetBytes(
  string chars,/*from   ww w.ja v a2s . c  o m*/
  int charIndex,
  int charCount,
  byte[] bytes,
  int byteIndex
)

Parameters

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

  • chars - The String 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(String, Int32, Int32, Byte[], Int32) method returns The actual number of bytes written into bytes.

Example


using System;//ww w  .  j  a v  a2s  .c  o m
using System.Text;

class ASCIIEncodingExample {
    public static void Main() {
        Byte[] bytes;
        Char[] chars = new Char[] {
            '\u0023', // #
            '\u0025', // %
            '\u03a0', // Pi
            '\u03a3'  // Sigma
        };

        ASCIIEncoding ascii = new ASCIIEncoding();

        int byteCount = ascii.GetByteCount(chars, 1, 2);
        bytes = new Byte[byteCount];
        int bytesEncodedCount = ascii.GetBytes(chars, 1, 2, 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