wcrtomb - C wchar.h

C examples for wchar.h:wcrtomb

Type

function

From

<cwchar>
<wchar.h>

Description

Convert wide character to multibyte sequence

Prototype

size_t wcrtomb (char* pmb, wchar_t wc, mbstate_t* ps);

Parameters

Parameter Description
pmb an array to hold a multibyte sequence.
wc Wide character of type wchar_t.
ps a conversion state.

Return Value

The number of bytes written at pmb.

on error, the function returns -1 and sets errno to EILSEQ.

Demo Code


#pragma warning(disable:4996)//from   w  w w .  j a va  2  s  . com
#define _CRT_SECURE_NO_WARNINGS

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

int main() {
  const wchar_t* pt = L"this is a wcrtomb example";
  char buffer[123];
  size_t length, i;
  mbstate_t mbs;

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

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

  return 0;
}

Related Tutorials