【发布时间】:2017-01-29 14:18:10
【问题描述】:
我一直在寻找将 unicode 代码点转换为 utf8 的方法。 到目前为止,我已经知道我可以手动完成,也可以使用 iconv。
我也认为 wctomb 会起作用,但它没有:
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#define CENTER_UTF8 "\xf0\x9d\x8c\x86"
#define CENTER_UNICODE 0x1D306
int main(int argc, char** argv)
{
puts(CENTER_UTF8); //OK
static char buf[10];
int r;
#define WCTOMB(What) \
wctomb(NULL,0); \
r=wctomb(buf,What); \
puts(buf); \
printf("r=%d\n", r);
//Either one fails with -1
WCTOMB(CENTER_UNICODE);
WCTOMB(htonl(CENTER_UNICODE));
}
有人可以向我解释为什么 wctomb 不会将 unicode 代码点转换为 utf8。我在使用 utf8 语言环境的 Linux 上。
【问题讨论】: