当字符集为ASCII时,如何在文字字符串ISO/ANSI C中表示Unicode字符?

Cha*_*ens 6 c unicode

在Perl中,我可以说

my $s = "r\x{e9}sum\x{e9}";
Run Code Online (Sandbox Code Playgroud)

分配"résumé"$s.我想在C中做类似的事情.具体来说,我想说

sometype_that_can_hold_utf8 c = get_utf8_char();
if (c < '\x{e9}') {
    /* do something */
}
Run Code Online (Sandbox Code Playgroud)

pax*_*blo 10

对于UTF8,您必须使用找到的规则自行生成编码,例如,此处.例如,德语sharp s(ß,代码点0xdf)具有UTF8编码0xc3,0x9f.您的e-acute(é,代码点0xe9)的UTF8编码为0xc3,0xa9.

你可以在你的字符串中添加任意十六进制字符:

char *cv = "r\xc3\xa9sum\xc3\xa9";
char *sharpS = "\xc3\x9f";
Run Code Online (Sandbox Code Playgroud)


pmg*_*pmg 6

如果您有C99编译器,则可以使用<wchar.h>(和<locale.h>)并直接在源代码中输入Unicode代码点.

$ cat wc.c

#include <locale.h>
#include <stdio.h>
#include <wchar.h>

int main(void) {
  const wchar_t *name = L"r\u00e9sum\u00e9";
  setlocale(LC_CTYPE, "en_US.UTF-8");
  wprintf(L"name is %ls\n", name);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

$ /usr/bin/gcc -std=c99 -pedantic -Wall wc.c

$ ./a.out

name is résumé
Run Code Online (Sandbox Code Playgroud)