function
<cuchar>

c16rtomb

size_t c16rtomb ( char * pmb, char16_t c16, mbstate_t * ps );
Convert 16-bit character to multibyte sequence
The 16-bit character c16 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_16__ is defined, c16 shall follow UTF-16 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 c16 is a null 16-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 c16).

This is the char16_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.
c16
16-bit character of type char16_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
/* c16rtomb example */
#include <wchar.h>
#include <uchar.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
  const char16_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 = c16rtomb(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