Unicode on C
示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[]) {
char * smile = "😊";
printf("%s\n", smile);
printf("len:%ld\n", strlen(smile));
for (int i=0; i<strlen(smile); i++) {
printf("%X\t",smile[i]);
}
puts("");
return EXIT_SUCCESS;
}
输出:
😊
len:4
FFFFFFF0 FFFFFF9F FFFFFF98 FFFFFF8A
- 每个字节多出来的『F』
一个字节8位,能存2个Hex值,但打印出了8个Hex。
“%X”本来用于打印十六进制无符号整型,这里的smile[i]是一个 char
,也就是 short short int
,应该使用”%hhX”。
~ 本来的 char
隐式提升为 unsigned int
,smile[i]的高位为 b1
,多出的高位自动补1,用十六进制表示也就是FFFFFF。 ~
输出变量在内存中的值的正确做法是:
int a = 123;
printf("%X", *(unsigned int *)&a);
/* printf example */
#include <stdio.h>
int main()
{
printf ("Characters: %c %c \n", 'a', 65);
printf ("Decimals: %d %ld\n", 1977, 650000L);
printf ("Preceding with blanks: %10d \n", 1977);
printf ("Preceding with zeros: %010d \n", 1977);
printf ("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
printf ("Width trick: %*d \n", 5, 10);
printf ("%s \n", "A string");
return 0;
}
Characters: a A
Decimals: 1977 650000
Preceding with blanks: 1977
Preceding with zeros: 0000001977
Some different radices: 100 64 144 0x64 0144
floats: 3.14 +3e+000 3.141600E+000
Width trick: 10
A string