The c16rtomb() function is defined in <cuchar> header file.
c16rtomb() Prototype
size_t c16rtomb(char* s, char16_t c16, mbstate_t* ps);
The c16rtomb() function converts the utf-16 character c16 to its multibyte equivalent and store it in the object pointed to by s.
If s represents a null pointer, the call is equivalent to c16rtomb(buf, u'\0', ps) for some internal buffer buf.
If c16 is the null wide character i.e. u'\0', a null byte is stored.
c16rtomb() Parameters
- s: Pointer to a character array where the multibyte character is to be stored.
- c16: The 16 bit character to convert.
- ps: A pointer to an mbstate_t object used when interpreting the multibyte string.
c16rtomb() Return value
- On success, the c16rtomb() function returns the number of bytes written to the character array pointed to by s.
- On failure, -1 is returned and EILSEQ is stored in errno.
Example: How c16rtomb() function works?
#include <cuchar>
#include <iostream>
using namespace std;
int main()
{
const char16_t str[] = u"Hello World!";
char s[50];
mbstate_t ps{};
size_t length;
int j = 0;
while (str[j])
{
length = c16rtomb(s, str[j], &ps);
if ((length == 0) || (length > 50))
break;
for (int i=0; i<length; ++i)
cout << s[i];
++j;
}
return 0;
}
When you run the program, the output will be:
Hello World!