Table of Contents

mbtowc

    #include <stdlib.h>
        int mbtowc(whcar_t *pwc, const char *str, size_t n);
 

Description

converts a multibyte character into a wide character.

  <setlocale.h> <wchar.h> is for this example nessesary

mbtowc C Sourcecode Example

/* 
 * mbtowc example code
 * http://code-reference.com/c/stdlib.h/mbtowc 
 */
#include <stdio.h>
#include <stdlib.h> 
#include <wchar.h> /*  wint_t*/
#include <locale.h> /* setlocale*/
 
int main(void)
{
  wchar_t wchar;
  char string[] = "mbtowc: 測試字符串";
  int i=0, length=0;
 
  setlocale(LC_ALL,"");  
  while ( ( length = mbtowc ( &wchar, &string[i], MB_CUR_MAX )) != 0)
    {
      printf( "%lc", (wint_t)wchar );
      //  printf("i=%i, length=%i\n",i, length);
      i += length;
    }
  printf("\n");
return 0;
}

output of mbtowc example

  
  mbtowc: 測試字符串