mbrlen - C wchar.h

C examples for wchar.h:mbrlen

Type

function

From


<cwchar>
<wchar.h>

Description

Get length of multibyte character

Prototype

size_t mbrlen (const char* pmb, size_t max, mbstate_t* ps);

Parameters

Parameter Description
pmb Pointer to a multibyte character.
max Maximum number of bytes to check.
ps Pointer to a mbstate_t object which sets the conversion state.

Return Value

On success, the size in bytes of that multibyte character is returned.

On error, -1 is returned and sets errno to EILSEQ.

Demo Code


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


int main()//from w  ww . java 2 s .  c o  m
{
  const char str [] = "this is a test string";

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

  size_t length;
  wchar_t dest;
  mbstate_t mbs;
  int i;

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

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

  return 0;
}

Related Tutorials