Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C Amendment |
both | z/OS® V2R1 |
#include <uchar.h>
size_t c16rtomb(char * restrict s, char16_t c16, mbstate_t * restrict ps);
The c16rtomb() function converts a wide character of type char16_t to a multibyte character, and returns the number of bytes stored in s (including any shift sequences).
If s is not a null pointer, the c16rtomb() function determines the number of bytes needed to represent the multibyte character that corresponds to the wide character given by c16 (including any shift sequences), and stores the multibyte character representation in the array whose first element is pointed to by s. At most MB_CUR_MAX bytes are stored. If the c16 is a null wide character, a null byte is stored, preceded by any shift sequence needed to restore the initial shift state; the resulting state described is the initial conversion state.
If s is a null pointer, the c16rtomb() function is equivalent to the call c16rtomb(buf, L'\0', ps), where buf is an internal buffer.
If ps is a null pointer, c16rtomb() uses its own internal object to track the shift state. Otherwise *ps must be a valid mbstate_t object. An mbstate_t object *ps can be initialized to the initial state by assigning 0 to it, or by calling c16rtomb(NULL, L'\0', ps).
The c16rtomb() function returns the number of bytes stored in the array object (including any shift sequences). When c16 is not a valid wide character, an encoding error occurs. The function stores the vlaue of the macro EILSEQ in errno and returns (size_t)(-1); the conversion state is unspecified.
#include <stdio.h>
#include <stdlib.h>
#include <uchar.h>
int main(void)
{
char16_t in = u'a';
mbstate_t st = 0;
char out[MB_CUR_MAX];
int rc, i;
rc = c16rtomb(out, in, &st);
if (rc < 0) {
perror("c16rtomb() fails to convert");
exit(-1);
}
printf(" c16: 0x%04x \n", in);
printf(" return code: %d \n", rc);
printf(" mb character: ");
for (i=0; i<rc; i++)
printf(" 0x%02x", out[i]);
printf("\n");
return 0;
}
c16: 0x0061
return code: 1
mb character: 0x81