Encodes a set of characters from the specified string into the specified byte array.
- 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.
The actual number of bytes written into bytes.
Type Reason 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.
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 ("?").
For security reasons, your application is recommended to use System.Text.UTF8Encoding, System.Text.UnicodeEncoding, or System.Text.UTF32Encoding and enable error detection.