Name

mbrtowc - converts a multibyte sequence to a wide character

Library

libc.lib

Synopsis

  #include <wchar.h>
  size_t mbrtowc (wchar_t * restrict dst, const char * restrict src, size_t n, mbstate_t * restrict ps);

Return values

The mbrtowc functions returns:
0 The next n or fewer bytes represent the null wide character (L’\0’).
>0 The next n or fewer bytes represent a valid character, mbrtowc returns the number of bytes used to complete the multibyte character.
(size_t-2)
  The next n contribute to, but do not complete, a valid multibyte character sequence, and all n bytes have been processed.
(size_t-1)
  An encoding error has occurred. The next n or fewer bytes do not contribute to a valid multibyte character.

Detailed description

The mbrtowc function inspects at most n bytes pointed to by s to determine the number of bytes needed to complete the next multibyte character. If a character can be completed, and pwc is not NULL, the wide character which is represented by s is stored in the wchar_t it points to.

If s is NULL, mbrtowc behaves as if pwc was NULL, s was an empty string ("") and n was 1.

The mbstate_t argument, ps, is used to keep track of the shift state. If it is NULL, mbrtowc uses an internal, static mbstate_t object, which is initialized to the initial conversion state at program startup.

The behavior of the mbrtowc is affected by LC_CTYPE category of the current locale.


Examples

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
/* Illustrates how to use mbrtowc API */
size_t example_mbrtowc()
{
 size_t len;
 wchar_t wc[100];
 char *s = ’a’;
 size_t n = 1;
 mbstate_t ps;
 /* converting multibyte sequence to a wide-char sequence */
 len = mbrtowc(wc,(const char *) s, n, &ps);
 /* checking for error value */
 if(len < 0)
 {
        wprintf(L"mbrtowc returned error!!\n");
 }
 /* returning no of bytes consumed */
 return (len);
}

         


Errors

The mbrtowc function will fail if:
[EILSEQ]
  An invalid multibyte sequence was detected.
[EINVAL]
  The conversion state is invalid.

Limitations

The current implementation of mbrtowc is not affected by the LC_CTYPE category of the current locale. It works only for UTF8 character set.

See also

mbtowc, setlocale, wcrtomb

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top