简体   繁体   English

C / C ++以十六进制打印字节,获取奇怪的十六进制值

[英]C/C++ Printing bytes in hex, getting weird hex values

I am using the following to print out numbers from an array in hex: 我正在使用以下内容从十六进制数组中打印出数字:

char buff[1000];

// Populate array....

int i;
for(i = 0; i < 1000; ++i)
{
    printf("[%d] %02x\n", i,buff[i]);
}

but I sometimes print weird values: 但是我有时会打印出奇怪的值:

byte[280] 00     
byte[281] 00     
byte[282] 0a     
byte[283] fffffff4         // Why is this a different length?
byte[284] 4e      
byte[285] 66      
byte[286] 0a   

Why does this print 'fffffff4'? 为什么打印“ fffffff4”?

Use %02hhx as the format string. 使用%02hhx作为格式字符串。

From CppReference , %02x accepts unsigned int . CppReference中%02x接受unsigned int When you pass the arguments to printf() , which is a variadic function , buff[i] is automatically converted to int . 当您将参数传递给可变参数函数 printf()buff[i]将自动转换为int Then the format specifier %02x makes printf() interprets the value as int , so potential negative values like (char)-1 get interpreted and printed as (int)-1 , which is the cause of what you observed. 然后,格式说明符%02x使printf()将该值解释为int ,因此可能的负值(char)-1(char)-1被解释并打印为(int)-1 ,这就是您所观察到的原因。

It can also be inferred that your platform has signed char type, and a 32-bit int type. 也可以推断出您的平台已签名char类型和32位int类型。

The length modifier hh will tell printf() to interpret whatever supplied as char type, so %hhx is the correct format specifier for unsigned char . 长度修饰符hh将告诉printf()解释以char类型提供的任何内容,因此%hhxunsigned char的正确格式说明符。

Alternatively, you can cast the data to unsigned char before printing. 或者,您可以在打印之前将数据转换为unsigned char Like 喜欢

printf("[%d] %02x\n", i, (unsigned char)buff[i]);

This can also prevent negative values from showing up as too long, as int can (almost) always contain unsigned char value. 由于int可以(几乎)始终包含unsigned char值,因此这也可以防止出现负值的时间过长。


See the following example: 请参见以下示例:

#include <stdio.h>

int main(){
    signed char a = +1, b = -1;
    printf("%02x %02x %02hhx %02hhx\n", a, b, a, b);
    return 0;
}

The output of the above program is: 上面程序的输出是:

01 ffffffff 01 ff

Your platform apparantly has signed char . 您的平台显然已签署了char On platforms where char is unsigned the output would be f4 . char不带符号的平台上,输出将为f4

When calling a variadic function any integer argument smaller than int gets promoted to int . 调用可变参数函数时,任何小于int整数参数都将提升为int

A char value of f4 ( -12 as a signed char ) has the sign bit set, so when converted to int becomes fffffff4 (still -12 but now as a signed int ) in your case. 一个f4char值( -12为一个有signed char )已设置了符号位,因此在您的情况下,转换为int时将变为fffffff4 (仍为-12但现在作为一个有signed int )。

%x02 causes printf to treat the argument as an unsigned int and will print it using at least 2 hexadecimal digits. %x02使printf将参数视为unsigned int ,并将使用至少 2个十六进制数字进行打印。 The output doesn't fit in 2 digits, so as many as are required are used. 输出的两位数不合适,因此使用了所需的位数。

Hence the output fffffff4 . 因此输出fffffff4

To fix it, either declare your array unsigned char buff[1000]; 要解决此问题,请声明数组unsigned char buff[1000];否则,请执行以下操作unsigned char buff[1000]; or cast the argument: 或强制转换参数:

    printf("[%d] %02x\n", i, (unsigned char)buff[i]);

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

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