字转换:wctomb、mbtowc,wc 指 Wide charactor,mb 指 Multi-byte。 字符串转换:wcstombs、mbstowcs,wcs 和 mbs 的 s 指 string。
这 4 个函数是 C 标准函数库函数中的。如果只是在 Windows 平台下编程,可直接调用 Windows API 函数 WideCharToMultiByte 和 MultiByteToWideChar 实现。但是如果调用标准库函数的话,在 Linux 下也是有效的。调用标准库函数,首先必须包含 locale.h 并调用 setlocale(LC_ALL, "") 后才能正确转换。Windows 下的 Multi-byte 是 ANSI 编码的,Wide charactor 是 Unicode (UTF-16) 编码,而 Linux 下的 Multi-byte 是 UTF-8 编码的,Wide charactor 是 Unicode (UTF-32) 编码。
#include <stdio.h> #include <stdlib.h> #include <locale.h>
int main(void) { char str[12]; wchar_t wstr[] = { 0x52B3, 0x788C, 0 }; setlocale(LC_ALL, ""); wcstombs(str, wstr, sizeof(str)/sizeof(char)); printf("%s", str); return 0; }
|