【发布时间】:2009-09-14 14:12:51
【问题描述】:
在 Perl 中,我可以说
my $s = "r\x{e9}sum\x{e9}";
将"résumé" 分配给$s。我想在C中做类似的事情。具体来说,我想说
sometype_that_can_hold_utf8 c = get_utf8_char();
if (c < '\x{e9}') {
/* do something */
}
【问题讨论】:
在 Perl 中,我可以说
my $s = "r\x{e9}sum\x{e9}";
将"résumé" 分配给$s。我想在C中做类似的事情。具体来说,我想说
sometype_that_can_hold_utf8 c = get_utf8_char();
if (c < '\x{e9}') {
/* do something */
}
【问题讨论】:
对于 UTF8,您必须使用找到的规则自己生成编码,例如 here。例如,德语的 s (ß, 代码点 0xdf) 的 UTF8 编码为 0xc3,0x9f。您的 e-acute(é,代码点 0xe9)的 UTF8 编码为 0xc3,0xa9。
您可以在字符串中添加任意十六进制字符:
char *cv = "r\xc3\xa9sum\xc3\xa9";
char *sharpS = "\xc3\x9f";
【讨论】:
如果您有 C99 编译器,则可以使用
$ 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;
}
$ /usr/bin/gcc -std=c99 -pedantic -Wall wc.c
$ ./a.out
name is résumé
【讨论】:
wchar_t 不一定能够同时表示多个语言环境。它只需要足够宽以支持当前的语言环境。
size_t __ctype_get_mb_cur_max()
wchar_t 是您要查找的类型:http://opengroup.org/onlinepubs/007908799/xsh/wchar.h.html
【讨论】:
"\x{e9}" 来实现。问题是源是ASCII,但是需要创建UTF-8字符。
??= 代表#)。请注意,源代码正在通过该系统,而不是在那里编译。是的,我知道这很愚蠢。
wchar_tsetlocale() 似乎可选
#include <stdio.h>
int main(void) {
const char *const name = "r\u00e9sum\u00e9";
printf("name is %s\n",name);
return 0;
}
$ echo $LANG
en_US.UTF-8
$ /usr/bin/gcc -std=c99 -pedantic -Wall -Wextra bc.c
$ ./a.out
name is résumé
【讨论】: