function
<cuchar>

c32rtomb

size_t c32rtomb ( char * pmb, char32_t c32, mbstate_t * ps );
Convert 32-bit character to multibyte sequence
The 32-bit character c32 is translated to its multibyte equivalent and stored in the array pointed by pmb. The function returns the length in bytes of the equivalent multibyte sequence pointed by pmb.

If the __STD_UTF_32__ is defined, c32 shall follow UTF-32 encoding.

The function uses (and updates) the shift state described by ps. If ps is a null pointer, the function uses its own internal shift state, which is altered as necessary only by calls to this function.

If c32 is a null 32-bit character, the function resets the shift state and stores a null byte, preceded by any shift sequence needed to restore the initial shift state.

A call to the function with a null pointer as pmb also resets the shift state (and ignores parameter c32).

This is the char32_t version of wcrtomb (<cwchar>).

Parameters

pmb
Pointer to an array large enough to hold a multibyte sequence.
The maximum length of a multibyte sequence for a character in the current locale is MB_CUR_MAX bytes.

Alternativelly, the function may be called with a null pointer, in which case the function resets the shift state (either ps or its own internal state) to the initial state and returns zero.
c32
32-bit character of type char32_t.
ps
Pointer to a mbstate_t object that defines a conversion state.

Return Value

The size of the multibyte sequence written at pmb (in bytes), including any shift characters. This may be zero.

If there is no character correspondence, the function returns (size_t)-1 and sets errno to EILSEQ.

If pmb is a null pointer, the function stores no bytes at pmb, and thus returns zero.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* c32rtomb example */
#include <wchar.h>
#include <uchar.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
  const char32_t* pt = U"Juan Souli\u00e9";
  char buffer [MB_CUR_MAX];
  int i;
  size_t length;
  mbstate_t mbs;

  mbrlen (NULL,0,&mbs);   /* initialize mbs */

  while (*pt) {
    length = c32rtomb(buffer,*pt,&mbs);
    if ((length==0)||(length>MB_CUR_MAX)) break;
    for (i=0;i<length;++i) putchar (buffer[i]);
    ++pt;
  }

  return 0;
}

Possible output:

Juan SouliƩ


See also