Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C Amendment |
both | z/OS® V2R1 |
#include <uchar.h>
size_t c32rtomb(char * restrict s, char32_t c32, mbstate_t * restrict ps);
The c32rtomb() function converts a wide character of type char32_t to a multibyte character, and returns the number of bytes stored in the s (including any shift sequences).
If s is not a null pointer, the c32rtomb() function determines the number of bytes needed to represent the multibyte character that corresponds to the wide character given by c32 (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 c32 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 c32rtomb() function is equivalent to the call c32rtomb(buf, L'\0', ps), where buf is an internal buffer.
If ps is a null pointer, c32rtomb() 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 c32rtomb(NULL, L'\0', ps).
The c32rtomb() function returns the number of bytes stored in the array object (including any shift sequences). When c32 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)
{
char32_t in = U'a';
mbstate_t st = 0;
char out[MB_CUR_MAX];
int rc, i;
rc = c32rtomb(out, in, &st);
if (rc < 0) {
perror("c32rtomb() fails to convert");
exit(-1);
}
printf(" c32: 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;
}
c32: 0x00000061
return code: 1
mb character: 0x81