Encoder::Convert Method (array<Char>^, Int32, Int32, array<Byte>^, Int32, Int32, Boolean, Int32%, Int32%, Boolean%)

 

Converts an array of Unicode characters to an encoded byte sequence and stores the result in an array of bytes.

Namespace:   System.Text
Assembly:  mscorlib (in mscorlib.dll)

public:
[ComVisibleAttribute(false)]
virtual void Convert(
	array<wchar_t>^ chars,
	int charIndex,
	int charCount,
	array<unsigned char>^ bytes,
	int byteIndex,
	int byteCount,
	bool flush,
	[OutAttribute] int% charsUsed,
	[OutAttribute] int% bytesUsed,
	[OutAttribute] bool% completed
)

Parameters

chars
Type: array<System::Char>^

An array of characters to convert.

charIndex
Type: System::Int32

The first element of chars to convert.

charCount
Type: System::Int32

The number of elements of chars to convert.

bytes
Type: array<System::Byte>^

An array where the converted bytes are stored.

byteIndex
Type: System::Int32

The first element of bytes in which data is stored.

byteCount
Type: System::Int32

The maximum number of elements of bytes to use in the conversion.

flush
Type: System::Boolean

true to indicate no further data is to be converted; otherwise, false.

charsUsed
Type: System::Int32%

When this method returns, contains the number of characters from chars that were used in the conversion. This parameter is passed uninitialized.

bytesUsed
Type: System::Int32%

When this method returns, contains the number of bytes that were produced by the conversion. This parameter is passed uninitialized.

completed
Type: System::Boolean%

When this method returns, contains true if all the characters specified by charCount were converted; otherwise, false. This parameter is passed uninitialized.

Exception Condition
ArgumentNullException

chars or bytes is null (Nothing).

ArgumentOutOfRangeException

charIndex, charCount, byteIndex, or byteCount is less than zero.

-or-

The length of chars - charIndex is less than charCount.

-or-

The length of bytes - byteIndex is less than byteCount.

ArgumentException

The output buffer is too small to contain any of the converted input. The output buffer should be greater than or equal to the size indicated by the GetByteCount method.

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 Convert. When the application is done with a stream of data, it should set the flush parameter to true to make sure that the state information is flushed. With this setting, the encoder ignores invalid bytes at the end of the data block and clears the internal buffer. Any remaining processed data that is part of a logical unit, such as the high surrogate of a surrogate pair, is converted according to the current fallback settings.

The Convert method is designed to be used in a loop to decode an arbitrary amount of input, such as data read from a file or stream. It stores the output of the encoding operation in a fixed-size buffer. 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 completed output parameter indicates whether all the data in the input buffer was converted and stored in the output buffer. This parameter is set to false if the number of characters specified by the charCount parameter cannot be converted without exceeding the number of bytes specified by the byteCount parameter. In that situation, the application should use the contents of the output buffer or provide a new output buffer, increment the chars parameter by the number of characters specified by the charsUsed parameter, then call the Convert method again to process the remaining input.

The completed parameter can also be set to false, even though the charsUsed and charCount parameters are equal. This situation occurs if there is still data in the Encoder object that has not been stored in the chars buffer.

The following example uses the Convert method to convert a file of UTF-16 characters to UTF-8, then uses the Convert method to convert the UTF-8 characters back to UTF-16 characters.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
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
Return to top
Show: