Utf8.FromUtf16 Method

Definition

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

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

Parameters

source
ReadOnlySpan<Char>

A UTF-16 encoded read-only character span.

destination
Span<Byte>

A UTF-8 encoded byte span.

charsRead
Int32

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

bytesWritten
Int32

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

replaceInvalidSequences
Boolean

true to replace invalid UTF-16 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 that status of the conversion.

Remarks

This method corresponds to the UTF8Encoding.GetBytes 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