Encoder::GetBytes Method (array<Char>^, Int32, Int32, array<Byte>^, Int32, Boolean)
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.
Assembly: mscorlib (in mscorlib.dll)
public: virtual int GetBytes( array<wchar_t>^ chars, int charIndex, int charCount, array<unsigned char>^ bytes, int byteIndex, bool flush ) abstract
Parameters
- chars
-
Type:
array<System::Char>^
The character array containing the set of characters to encode.
- charIndex
-
Type:
System::Int32
The index of the first character to encode.
- charCount
-
Type:
System::Int32
The number of characters to encode.
- bytes
-
Type:
array<System::Byte>^
The byte array to contain the resulting sequence of bytes.
- byteIndex
-
Type:
System::Int32
The index at which to start writing the resulting sequence of bytes.
- flush
-
Type:
System::Boolean
true to clear the internal state of the encoder after the conversion; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | chars is null (Nothing). -or- bytes is null (Nothing). |
| ArgumentOutOfRangeException | charIndex or charCount or byteIndex is less than zero. -or- charIndex and charCount do not denote a valid range in chars. -or- byteIndex is not a valid index in bytes. |
| ArgumentException | bytes does not have enough capacity from byteIndex to the end of the array to accommodate the resulting bytes. |
| EncoderFallbackException | A fallback occurred (see Character Encoding in the .NET Framework for fuller explanation) -and- Fallback is set to EncoderExceptionFallback. |
Remember that the Encoder object saves state between calls to GetBytes. 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 GetByteCount.
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 Convert method. GetBytes will throw an exception if the output buffer isn't large enough, but Convert will fill as much space as possible and return the chars read and bytes written. Also see the Encoding::GetBytes topic for more comments.
The following example demonstrates how to encode a range of elements from a character array and store the encoded bytes in a range of elements in a byte array. The GetByteCount method is used to determine the size of the array required by GetBytes.
using namespace System; using namespace System::Text; using namespace System::Collections; int main() { array<Byte>^bytes; // Unicode characters. // Pi // Sigma array<Char>^chars = {L'\u03a0',L'\u03a3',L'\u03a6',L'\u03a9'}; Encoder^ uniEncoder = Encoding::Unicode->GetEncoder(); int byteCount = uniEncoder->GetByteCount( chars, 0, chars->Length, true ); bytes = gcnew array<Byte>(byteCount); int bytesEncodedCount = uniEncoder->GetBytes( chars, 0, chars->Length, bytes, 0, true ); Console::WriteLine( "{0} bytes used to encode characters.", bytesEncodedCount ); Console::Write( "Encoded bytes: " ); IEnumerator^ myEnum = bytes->GetEnumerator(); while ( myEnum->MoveNext() ) { Byte b = safe_cast<Byte>(myEnum->Current); Console::Write( "[{0}]", b ); } Console::WriteLine(); } /* This code example produces the following output. 8 bytes used to encode characters. Encoded bytes: [160][3][163][3][166][3][169][3] */
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1