宽字节 多字节 mbstowcs wcstombs

函数

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\n", size); 
printf("wbuf = %ls\n", wbuf);
//wprintf(L"%S\n",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\n", size); 
printf("buf = %s\n", buf);
# ./a.out 
size = 9
buf = 我很好
posted @ 2017-05-04 22:13  thomas_blog  阅读(139)  评论(0编辑  收藏  举报