c32rtomb() — Convert a char32_t character to a multibyte character

Standards

Standards / Extensions C or C++ Dependencies

ISO C Amendment
C11

both z/OS® V2R1

Format

#include <uchar.h>

size_t c32rtomb(char * restrict s, char32_t c32, mbstate_t * restrict ps);

General description

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).

Usage notes

  1. To use the c32rtomb() function, compile the source code with the LANGLVL(EXTC1X) option.
  2. The result s for stateful multibyte encodings, such as EBCDIC MBCS, might leave out shift bytes according to the conversion state. The first DBCS character in the output sequence has only shift-out character, and the following characters have neither shift-out nor shift-in. The ending shift-in will not be produced until an SBCS character or a null wide character is encountered.
  3. The c32rtomb() function only supports the CCSIDs that are provided by Unicode Services.
  4. The Unicode combining characters are not supported, and will be converted to substitute character of target CCSID.
  5. The result of converting multiple string alternately in one thread by using multiple mbstate_t objects (including the internal one) is undefined.

Returned value

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.

Example

#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;
}
Output:
c32: 0x00000061
return code: 1
mb character:  0x81

Related information