映射多字节字符自己的UNI code点再presentation [英] Mapping multibyte characters to their unicode point representation

查看:107
本文介绍了映射多字节字符自己的UNI code点再presentation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何映射一个UTF-8字符在C UNI其code点?
[例如,电子将映射到 00C8

How do you map a single UTF-8 character to its unicode point in C? [For example, È would be mapped to 00c8].

推荐答案

如果您的平台的 wchar_t的商店UNI code(如果它是一个32位的类型,可能做),你有一个使用UT​​F-8,你可以叫 mbrtowc (自C90.1)。

If your platform's wchar_t stores unicode (if it's a 32-bit type, it probably does) and you have an UTF-8 locale, you can call mbrtowc (from C90.1).

mbstate_t state = {0};
wchar_t wch;
char s[] = "\303\210";
size_t n;
memset(&state, 0, sizeof(state));
setlocale(LC_CTYPE, "en_US.utf8"); /*error checking omitted*/
n = mbrtowc(&wch, s, strlen(s), &state);
if (n <= (size_t)-2) printf("%lx\n", (unsigned long)wch);

有关更多的灵活性,你可以调用的iconv 接口。

For more flexibility, you can call the iconv interface.

char s[] = "\303\210";
iconv_t cd = iconv_open("UTF-8", "UCS-4");
if (cd != -1) {
    char *inp = s;
    size_t ins = strlen(s);
    uint32_t c;
    uint32_t *outp = &c;
    size_t outs = 0;
    if (iconv(cd, &inp, &ins, &outp, &outs) + 1 >= 2) printf("%lx\n", c);
    iconv_close(cd);
}

这篇关于映射多字节字符自己的UNI code点再presentation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆