Edit

Share via


Utf8.ToUtf16 Method

Definition

Converts a UTF-8 encoded read-only byte span to a UTF-16 encoded character span.

public static System.Buffers.OperationStatus ToUtf16 (ReadOnlySpan<byte> source, Span<char> destination, out int bytesRead, out int charsWritten, bool replaceInvalidSequences = true, bool isFinalBlock = true);
static member ToUtf16 : ReadOnlySpan<byte> * Span<char> * int * int * bool * bool -> System.Buffers.OperationStatus
Public Shared Function ToUtf16 (source As ReadOnlySpan(Of Byte), destination As Span(Of Char), ByRef bytesRead As Integer, ByRef charsWritten As Integer, Optional replaceInvalidSequences As Boolean = true, Optional isFinalBlock As Boolean = true) As OperationStatus

Parameters

source
ReadOnlySpan<Byte>

A UTF-8 encoded read-only byte span.

destination
Span<Char>

A UTF-16 encoded character span.

bytesRead
Int32

When the method returns, the number of bytes read from source.

charsWritten
Int32

When the method returns, the number of characters written to destination.

replaceInvalidSequences
Boolean

true to replace invalid UTF-8 sequences in source with U+FFFD; false to return InvalidData if invalid characters are found in source.

isFinalBlock
Boolean

true if the method should not return NeedMoreData; otherwise, false.

Returns

A value that indicates the status of the conversion.

Remarks

This method corresponds to the UTF8Encoding.GetChars method, except that it has a different calling convention, different error handling mechanisms, and different performance characteristics.

If 'replaceInvalidSequences' is true, the method replaces any ill-formed subsequences in source with U+FFFD in destination and continues processing the remainder of the buffer. Otherwise, the method returns OperationStatus.InvalidData if it encounters any ill-formed sequences.

If the method returns an error code, the out parameters indicate how much of the data was successfully transcoded, and the location of the ill-formed subsequence can be deduced from these values.

If 'replaceInvalidSequences' is true, the method never returns OperationStatus.InvalidData. If 'isFinalBlock' is true, the method never returns OperationStatus.NeedMoreData.

Applies to