c16rtomb() — Convert a char16_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 c16rtomb(char * restrict s, char16_t c16, mbstate_t * restrict ps);

General description

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

Usage notes

  1. To use the c16rtomb() 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 c16rtomb() 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 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.

Example

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

Related information