Name

mbsrtowcs, mbsnrtowcs
- convert a multibyte string to a wide character string

Library

libc.lib

Synopsis

  #include <wchar.h>
  size_t mbsrtowcs (wchar_t * restrict dst, const char ** restrict src, size_t len, mbstate_t * restrict ps);
  size_t mbsnrtowcs (wchar_t * restrict dst, const char ** restrict src, size_t nms, size_t len, mbstate_t * restrict ps);

Return values

The mbsrtowcs and mbsnrtowcs functions return the number of wide characters stored in the array pointed to by dst if successful, otherwise it returns (size_t-1.)

Detailed description

The mbsrtowcs function converts a sequence of multibyte characters pointed to indirectly by src into a sequence of corresponding wide characters and stores at most len of them in the wchar_t array pointed to by dst, until it encounters a terminating null character (’\0’.)

If dst is NULL, no characters are stored.

If dst is not NULL, the pointer pointed to by src is updated to point to the character after the one that conversion stopped at. If conversion stops because a null character is encountered, *src is set to NULL.

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

The mbsnrtowcs function behaves identically to mbsrtowcs, except that conversion stops after reading at most nms bytes from the buffer pointed to by src.

The behavior of the mbsrtowcs and mbsnrtowcs is affected by LC_CTYPE category of the current locale.


Examples

/* Illustrates how to use mbsrtowcs API */
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
size_t example_mbsrtowcs(wchar_t *wc, char *s, size_t n)
{
 size_t len;
 mbstate_t mbs;
 /* converting multibyte string to a wide-char string */
 len = mbsrtowcs(wc, &s, n, &mbs);
 /* checking for error */
 if(len < 0)
 {
        wprintf(L"mbsrtowcs returned error!!\n");
 }
 /* returning no of bytes consumed */
 return (len);
}

         

/* Illustrates how to use mbsrntowcs API */
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
size_t example_mbsnrtowcs(wchar_t *wc, char *s, size_t n, size_t nms)
{
  size_t len;
  mbstate_t mbs;
 /* converting multibyte string to a wide-char string */
  len = mbsnrtowcs(wc, &s, nms, n, &mbs);
 /* checking for error */
 if(len < 0)
 {
        wprintf(L"mbsnrtowcs returned error!!\n");
 }
  /* returning no of bytes consumed */
  return (len);
}

         


Errors

The mbsrtowcs and mbsnrtowcs functions will fail if:
[EILSEQ]
  An invalid multibyte character sequence was encountered.
[EINVAL]
  The conversion state is invalid.

Limitations

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

See also

mbrtowc, mbstowcs, wcsrtombs

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top