[#BloombergLP-bdlde-CharConvertUtf16-utf16ToUtf8-0813]
= xref:BloombergLP.adoc[BloombergLP]::xref:BloombergLP/bdlde.adoc[bdlde]::xref:BloombergLP/bdlde/CharConvertUtf16.adoc[CharConvertUtf16]::utf16ToUtf8
:relfileprefix: ../../../
:mrdocs:

Convert the UTF&hyphen;16 source to UTF&hyphen;8 in the destination&period;

== Synopsis

Declared in `&lt;bdlde&lowbar;charconvertutf16&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
static
int
utf16ToUtf8(
    char* dstBuffer,
    std::size&lowbar;t dstCapacity,
    unsigned short const* srcString,
    std::size&lowbar;t* numCodePointsWritten = 0,
    std::size&lowbar;t* numBytesWritten = 0,
    char errorByte = &apos;?&apos;,
    xref:BloombergLP/bdlde/ByteOrder/Enum.adoc[ByteOrder::Enum] byteOrder = ByteOrder&colon;&colon;e&lowbar;HOST);
----

== Description

Load, into the specified `dstBuffer` of the specified `dstCapacity`, the result of converting the specified UTF&hyphen;16 `srcString` to its UTF&hyphen;8 equivalent&period; Optionally specify `numCodePointsWritten`, which (if not 0) indicates the location of the modifiable variable into which the number of Unicode code points (including the terminating 0, if any) written is to be loaded, where one code point can occupy multiple bytes&period; Optionally specify `numBytesWritten`, which (if not 0) indicates the location of the modifiable variable into which the number of bytes written (including the null terminator, if any) is to be loaded&period; Optionally specify an `errorByte` to be substituted (if not 0) for invalid encodings in the input string&period; Invalid encodings are incomplete multi&hyphen;word encodings or parts of a two&hyphen;word encoding out of their proper sequence&period; If `errorByte` is 0, invalid input sequences are ignored (i&period;e&period;, produce no corresponding output)&period; Optionally specify `byteOrder` to indicate the byte order of the UTF&hyphen;16 input&semi; if `byteOrder` is not specified, the input is assumed to be in host byte order&period; Return 0 on success and a bitwise&hyphen;or of the flags defined by `CharConvertStatus&colon;&colon;Enum` otherwise&period; `CharConvertStatus&colon;&colon;k&lowbar;INVALID&lowbar;INPUT&lowbar;BIT` will be set if one or more invalid sequences were encountered in the input, and `CharConvertStatus&colon;&colon;k&lowbar;OUT&lowbar;OF&lowbar;SPACE&lowbar;BIT` will be set if the output space was exhausted before conversion was complete&period; The behavior is undefined unless `dstBuffer` refers to an array of at least `dstCapacity` elements, `errorByte` is either 0 or a valid single&hyphen;byte Unicode code point (`0 &lt; errorByte &lt; 0x80`), and `srcString` is null&hyphen;terminated if supplied as a pointer&period; Note that if `dstCapacity` is 0, this function returns `CharConvertStatus&colon;&colon;k&lowbar;OUT&lowbar;OF&lowbar;SPACE&lowbar;BIT` set and 0 is written into `&ast;numCodePointsWritten` and `&ast;numBytesWritten` (if those pointers are non&hyphen;null), since there is insufficient space for even a null terminator alone&period; Also note that since UTF&hyphen;8 is a variable&hyphen;length encoding, `numBytesWritten` may be up to four times `numCodePointsWritten`, and therefore that an input `srcString` of `dstCapacity` code points (including the terminating 0, if present) may not fit into `dstBuffer`&period; A one&hyphen;word (two&hyphen;byte) UTF&hyphen;16 code point will require one to three UTF&hyphen;8 octets (bytes)&semi; a two&hyphen;word (four&hyphen;byte) UTF&hyphen;16 code point will always require four UTF&hyphen;8 octets&period; Also note that the amount of room needed will vary with the contents of the data and the language being translated, but never will the number of bytes output exceed three times the number of words input&period; Also note that, if `dstCapacity &gt; 0`, then, after completion, `strlen(dstBuffer) &plus; 1 &equals;&equals; &ast;numBytesWritten`&period; Also note that if `srcString` is a `bslstl&colon;&colon;StringRef`, it may contain embedded 0 words that will be translated to null bytes embedded in the output&period;

== Return Value

0 on success and a bitwise&hyphen;or of the bits specified by `CharConvertStatus&colon;&colon;Enum` otherwise

== Parameters

[cols="1,4"]
|===
| Name| Description
| *dstBuffer*
| destination buffer for the converted output
| *dstCapacity*
| capacity of `dstBuffer` in elements
| *srcString*
| source string to convert
| *numCodePointsWritten*
| if non&hyphen;null, set to the code&hyphen;point count written
| *numBytesWritten*
| if non&hyphen;null, set to the byte count written
| *errorByte*
| replacement for invalid input, or 0 to skip
| *byteOrder*
| UTF&hyphen;16 byte order&semi; host order if unspecified
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
