如何获取字符的 Unicode 代码点?这是我尝试过的,但它没有打印相同的字符,我是否正确理解 unicode 的工作原理?
如何获取unicode字符的值?
#include <stdio.h>
int main()
{
char *a = "ā";
int n;
while(a[n] != '\0')
{
printf("%x", a[n]);
n+=1;
}
printf("\n \uC481");
return 0;
}
最佳答案
首先,您的代码中几乎没有更正。
#include <stdio.h>
int main()
{
char *a = "ā";
int n = 0; //Initialize n with zero.
while(a[n] != '\0')
{
printf("%x", a[n]);
n+=1;
}
//\u will not work. To print hexadecimal value, use \x
printf("\n %X\n\", 0xC481);
return 0;
}
在这里,您尝试打印每个字节的十六进制值。这将不是超过 0xff 的字符的 Unicode 值。
unsigned Short
是用于存储 Unicode 值的最常见的数据结构,尽管它不能存储所有代码点。如果您需要按原样存储所有 Unicode 点,请使用 int
,它必须是 32 位。
字符的 Unicode 值是每个字符以 UTF-32 表示时的数值。否则,如果编码是 UTF-8 或 UTF-16,则必须根据字节序列进行计算。
关于C 获取字符的unicode代码点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20449546/