System.Text.ASCIIEncoding.GetBytes Method

Encodes a set of characters from the specified string into the specified byte array.

Syntax

public override int GetBytes (string chars, int charIndex, int charCount, byte[] bytes, int byteIndex)

Parameters

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

The actual number of bytes written into bytes.

Exceptions

TypeReason
ArgumentException(bytes.Length - byteIndex) < charCount.
ArgumentNullException

chars is null.

-or-

bytes is null.

ArgumentOutOfRangeException

charIndex < 0.

-or-

charCount < 0.

-or-

(chars.Length - charIndex) < charCount.

-or-

byteIndex < 0.

-or-

byteIndex >= bytes.Length.

Remarks

To calculate the exact array size required by ASCIIEncoding.GetBytes(string, int, int, Byte[], int) to store the resulting bytes, the application uses ASCIIEncoding.GetByteCount(Char[], int, int). To calculate the maximum array size, the application should use ASCIIEncoding.GetMaxByteCount(int). The ASCIIEncoding.GetByteCount(Char[], int, int) method generally allows allocation of less memory, while the ASCIIEncoding.GetMaxByteCount(int) method generally executes faster.

Data to be converted, such as data read from a stream, can be available only in sequential blocks. In this case, or if the amount of data is so large that it needs to be divided into smaller blocks, the application should use the System.Text.Decoder or the System.Text.Encoder provided by the ASCIIEncoding.GetDecoder method or the ASCIIEncoding.GetEncoder method, respectively.

System.Text.ASCIIEncoding does not provide error detection. Any Unicode character greater than U+007F is encoded as the ASCII question mark ("?").

Note:

For security reasons, your application is recommended to use System.Text.UTF8Encoding, System.Text.UnicodeEncoding, or System.Text.UTF32Encoding and enable error detection.

Requirements

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0