c16rtomb - C uchar.h

C examples for uchar.h:c16rtomb

Type

function

from

<cuchar>
<uchar.h>

Description

Convert 16-bit character to multibyte sequence

Prototype

size_t c16rtomb ( char * pmb, char16_t c16, mbstate_t * ps );

Parameters

Parameter Description
pmb an array to hold a multibyte character.
c1616-bit character of type char16_t.
ps a mbstate_t object that defines a conversion state.

Return Value

On success, return the size of the multibyte sequence written at pmb in bytes.

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

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

Demo Code


#pragma warning(disable:4996)/*from   w ww.j a  va  2  s  .  co  m*/
#define _CRT_SECURE_NO_WARNINGS

#include <wchar.h>
#include <uchar.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
  const char16_t* pt = u"this is a test\u00e9";
  char buffer[123];

  size_t length;
  mbstate_t mbs;

  mbrlen(NULL, 0, &mbs);

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

  return 0;
}

Related Tutorials