mbrtowc - C wchar.h

C examples for wchar.h:mbrtowc

Type

function

from

<cwchar>

Description

Convert multibyte sequence to wide character

Prototype

size_t mbrtowc (wchar_t* pwc, const char* pmb, size_t max, mbstate_t* ps);

Parameters

Parameter Description
pwc Pointer to an object of type wchar_t.
pmb Pointer to the first byte of a multibyte character.
max Maximum number of bytes to read from pmb.
ps conversion state.

Return Value

The number of bytes used to create the wide character.

If the null wide character passed in, or if pmb is a null pointer, the function returns zero.

Demo Code


#include <wchar.h>

int main()//from  w ww  . j  av  a 2s .  c  om
{
  const char str[] = "this is a mbrtowc example";

  const char* pt = str;
  size_t max = sizeof(str);

  size_t length;
  wchar_t dest;
  mbstate_t mbs;

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

  while (max>0) {
    length = mbrtowc(&dest, pt, max, &mbs);
    if ((length == 0) || (length>max)) break;
    wprintf(L"[%lc]", dest);
    pt += length; max -= length;
  }

  return 0;
}

Related Tutorials