std::c16rtomb (3) - Linux Man Pages
Defined in header <cuchar>
std::size_t c16rtomb( char* s, char16_t c16, std::mbstate_t* ps ); (since C++11)
Converts a single code point from variable-length 16-bit character representation (typically, UTF-16) to a narrow multibyte character representation.
If s is not a null pointer and c16 is the last 16-bit code unit in a valid variable-length encoding of a code point, the function determines the number of bytes necessary to store the multibyte character representation of that code point (including any shift sequences, and taking into account the current multibyte conversion state *ps), and stores the multibyte character representation in the character array whose first element is pointed to by s, updating *ps as necessary. At most MB_CUR_MAX bytes can be written by this function.
If s is a null pointer, the call is equivalent to std::c16rtomb(buf, u'\0', ps) for some internal buffer buf.
If c16 is not the final code unit in a 16-bit representation of a wide character, it does not write to the array pointed to by s, only *ps is updated.
If c16 is the null wide character u'\0', a null byte is stored, preceded by any shift sequence necessary to restore the initial shift state and the conversion state parameter *ps is updated to represent the initial shift state.
The multibyte encoding used by this function is specified by the currently active C locale.
s - pointer to narrow character array where the multibyte character will be stored
c16 - the 16-bit character to convert
ps - pointer to the conversion state object used when interpreting the multibyte string
On success, returns the number of bytes (including any shift sequences) written to the character array whose first element is pointed to by s. This value may be 0, e.g. when processing the first char16_t in a surrogate pair.
On failure (if c16 is not a valid 16-bit character), returns -1, stores EILSEQ in errno, and leaves *ps in unspecified state.
The C++ standard defers to the C standard for the semantics of this function. In C11 as published, unlike std::mbrtoc16, which converts variable-width multibyte (such as UTF-8) to variable-width 16-bit (such as UTF-16) encoding, this function can only convert single-unit 16-bit encoding, meaning it cannot convert UTF-16 to UTF-8 despite that being the original intent of this function. This was corrected by the post-C11 defect report DR488.
Note: this example assumes the fix for the defect report 488 is applied
// Run this code
mbrtoc16 Converts a narrow multibyte character to UTF-16 encoding
do_out converts a string from internT to externT, such as when writing to file