linuxunicode
发布时间: 2023-07-03 05:15:13
1. 请求在linux下C语言如何将汉字转换成UTF
试试这个四个函数,C 里面的,Linux 可用:
mbtowc
wctomb
mbstowcs
wcstombs
在 Linux 下试试看吧:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main(void)
{
size_t cch;
char psz[1024];
wchar_t pwsz[] = { 0x52B3, 0x788C, 0x788C, 0 };
setlocale(LC_ALL, "");
cch = wcstombs(psz, pwsz, 1024);
if (cch != 0 && cch != -1) {
printf("%s", psz);
}
return 0;
}
zdl_361 说的 "utf8 劳碌碌" 不对,因为我也输出 "劳碌碌",而我是用 Unicode 编码的。在 Windows 上,char 是 ANSI,Unicode (wchar_t) 是 UTF-16;在 Linux 上,char 是 UTF-8,Unicode (wchar_t) 是 UTF-32。不过对于这个函数来说,在哪个平台上都不会因为字符编码而影响使用。
热点内容