C Library Function - mbtowc()
C Standard Library - <stdlib.h>
Description
The C library function int mbtowc(wchart *pwc, const char *str, sizet n) converts a multi-byte sequence to a wide character.
Declaration
Here is the declaration for the mbtowc() function.
int mbtowc(wchar_t *pwc, const char *str, size_t n)
Parameters
pwc -- Pointer to an object of type wchar_t.
str -- Pointer to the first byte of a multi-byte character.
n -- Maximum number of bytes to be checked.
Return Value
If str is not NULL, the mbtowc() function returns the number of bytes consumed from str, or 0 if it points to a null byte, or -1 if the operation fails.
If str is NULL, the mbtowc() function returns non-zero if the encoding has a shift state, or zero if the encoding is stateless.
Example
The following example demonstrates the use of the mbtowc() function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str = "这里是 tutorialpro.org";
wchar_t mb[100];
int len;
len = mblen(NULL, MB_CUR_MAX);
mbtowc(mb, str, len*strlen(str) );
wprintf(L"%ls \n", mb );
return(0);
}
Let's compile and run the above program, which will produce the following result as it outputs the result in multi-byte format, which is a binary output.
???