The mbstowcs
(“multibyte string to wide character string”)
function converts the null-terminated string of multibyte characters
string to an array of wide character codes, storing not more than
size wide characters into the array beginning at wstring.
The terminating null character counts towards the size, so if size
is less than the actual number of wide characters resulting from
string, no terminating null character is stored.
The conversion of characters from string begins in the initial
shift state.
If an invalid multibyte character sequence is found, the mbstowcs
function returns a value of -1. Otherwise, it returns the number
of wide characters stored in the array wstring. This number does
not include the terminating null character, which is present if the
number is less than size.
Here is an example showing how to convert a string of multibyte
characters, allocating enough space for the result.
wchar_t *
mbstowcs_alloc (const char *string)
{
size_t size = strlen (string) + 1;
wchar_t *buf = xmalloc (size * sizeof (wchar_t));
size = mbstowcs (buf, string, size);
if (size == (size_t) -1)
return NULL;
buf = xrealloc (buf, (size + 1) * sizeof (wchar_t));
return buf;
}