System.Text.Encoder.GetBytes Method

When overridden in a derived class, encodes a set of characters from the specified character array and any characters in the internal buffer into the specified byte array. A parameter indicates whether to clear the internal state of the encoder after the conversion.

Syntax

public abstract int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)

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.
flush
true to clear the internal state of the encoder after the conversion; otherwise, false.

Returns

The actual number of bytes written into bytes.

Exceptions

TypeReason
ArgumentException bytes does not contain sufficient space to store the encoded characters.
ArgumentNullException

chars is null .

-or-

bytes is null .

ArgumentOutOfRangeException

charIndex < 0.

-or-

charCount < 0.

-or-

byteIndex < 0.

-or-

(chars.Length - charIndex) < charCount.

-or-

byteIndex > bytes.Length.

Remarks

Remember that the System.Text.Encoder object saves state between calls to Encoder.GetBytes(Char[], int, int, Byte[], int, bool). When the application is done with a stream of data, it should set the flush parameter to true in the last call to GetBytes to make sure that the state information is flushed and that the encoded bytes are properly terminated. With this setting, the encoder ignores invalid bytes at the end of the data block, such as unmatched surrogates or incomplete combining sequences, and clears the internal buffer.

To calculate the exact buffer size that GetBytes requires to store the resulting characters, the application should use Encoder.GetByteCount(Char[], int, int, bool).

If GetBytes is called with flush set to false, the encoder stores trailing bytes at the end of the data block in an internal buffer and uses them in the next encoding operation. The application should call GetByteCount on a block of data immediately before calling GetBytes on the same block, so that any trailing characters from the previous block are included in the calculation.

If your application is to convert many segments of an input stream, consider using the Encoder.Convert(Char[], int, int, Byte[], int, int, bool, Int32@, Int32@, Boolean@) method. Encoder.GetBytes(Char[], int, int, Byte[], int, bool) will throw an exception if the output buffer isn't large enough, but Encoder.Convert(Char[], int, int, Byte[], int, int, bool, Int32@, Int32@, Boolean@) will fill as much space as possible and return the chars read and bytes written. Also see the Encoding.GetBytes(Char[], int, int, Byte[], int) topic for more comments.

Requirements

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