函数
size_t wcstombs(char *dest, const wchar_t *src, size_t n); //wide-character to a multibyte
n:被写入到 str 中的最大字节数
size_t mbstowcs(wchar_t *dest, const char *src, size_t n); //multibyte to wide-character
n:被转换的最大字符数
char *setlocale(int category, const char *locale);
设置当前程序使用的本地化信息
locale:语言代码_国家代码.编码。比如(zh_CN.UTF-8, en_US)
int wprintf(const wchar_t *format, ...);
int fwprintf(FILE *stream, const wchar_t *format, ...);
int swprintf(wchar_t *wcs, size_t maxlen,
const wchar_t *format, ...);
举例
char *str = "你好吗";
setlocale(LC_ALL, "en_US.utf8");
wchar_t wbuf[64] = {0};
int size = mbstowcs(wbuf, str, strlen(str));
printf("size = %d
", size);
printf("wbuf = %ls
", wbuf);
//wprintf(L"%S
",wbuf); //printf二选一
# ./a.out
size = 3
wbuf = 你好吗
wchar_t *wstr = L"我很好";
setlocale(LC_ALL, "en_US.utf8");
char buf[64] = {0};
int size = wcstombs(buf, wstr, sizeof(buf));
printf("size = %d
", size);
printf("buf = %s
", buf);
# ./a.out
size = 9
buf = 我很好