简体   繁体   English

带有printf()的c中的十六进制unicode

[英]Hexadecimal unicode in c with printf()

this is my code: 这是我的代码:

#include <stdlib.h>
#include <stdio.h>

int main(int argc,char **argv){
    printf("%x\n",*argv[1]);
    return 1;
}

after compile (gcc -o main main.c) , i run it and with no problem: 编译后(gcc -o main main.c) ,我运行它,没有问题:

./main 1
31

note: 31 is hex code of 1 注意: 311十六进制代码

but when run it with Unicode parameter, print 4Byte of hex: 但是当使用Unicode参数运行它时,输出4Byte的十六进制:

./main $(printf "\Udbb1")
ffffffed

what is problem? 有什么问题

There are two things at play here: 这里有两件事在起作用:

The first is that when you pass an argument to a variable-argument function like printf it might be promoted . 首先是,当您将参数传递给诸如printf之类的变量参数函数时,它可能会被提升 What happens here is that the character is promoted to an int value. 这里发生的是该字符被提升为int值。

And here the other thing comes in, namely sign extension . 这是另一件事,即符号扩展 If you convert a small signed type (like char seems to be on your system) to another larger signed type (like int ), if the smaller type is negative then the larger type has to be the same negative number. 如果将一个小的带符号类型(如char似乎在系统上)转换为另一个较大的带符号类型(如int ),则如果较小的类型为负数,则较大的类型必须为相同的负数。 And because of the way two's complement (which is the most common to handle negative numbers on binary systems) works the value will be filled up with leading binary ones. 并且由于二进制补码 (在二进制系统上处理负数最常见)的工作方式,该值将被前导二进制数填充。

If you want to print only the char value you need to use the printf format prefix hh as in "%hhx" . 如果只想打印char值,则需要使用printf格式前缀hh"%hhx" The value will then be treated as an unsigned char . 该值将被视为unsigned char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM