将多字节字符映射到其unicode点表示

如何将单个UTF-8字符映射到C中的unicode点? [例如, È将映射到00c8 ]。

如果您的平台的wchar_t存储unicode(如果它是32位类型,它可能存在)并且您具有UTF-8语言环境,则可以调用mbrtowc (来自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界面。

 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); } 

有些事情要看:

  • libiconv的
  • ConvertUTF.h
  • MultiByteToWideChar(在windows下)

UTF-8到UCS-2转换器的合理快速实现。 BMP之外的代理人和人物留下来作为锻炼。 该函数返回从输入s字符串中消耗的字节数。 负值表示错误。 生成的unicode字符放在地址p指向。

 int utf8_to_wchar(wchar_t *p, const char *s) { const unsigned char *us = (const unsigned char *)s; p[0] = 0; if(!*us) return 0; else if(us[0] < 0x80) { p[0] = us[0]; return 1; } else if(((us[0] & 0xE0) == 0xC0) && (us[1] & 0xC0) == 0x80) { p[0] = ((us[0] & 0x1F) << 6) | (us[1] & 0x3F); #ifdef DETECT_OVERLONG if(p[0] < 0x80) return -2; #endif return 2; } else if(((us[0] & 0xF0) == 0xE0) && (us[1] & 0xC0) == 0x80 && (us[2] & 0xC0) == 0x80) { p[0] = ((us[0] & 0x0F) << 12) | ((us[1] & 0x3F) << 6) | (us[2] & 0x3F); #ifdef DETECT_OVERLONG if(p[0] < 0x800) return -2; #endif return 3; } return -1; }