如何打印二进制文件的真实内容

时间:2018-10-20 14:26:53

标签: c binary printf fwrite fread

我使用下面的代码来测试读取的二进制文件,bin_data_file中的内容如下,它具有512个字节。我的问题是,为什么输出使用“ printf(“ buf为%02x \ n”,buf [i])“之类的ffffff8c包含奇怪的内容?如何打印buf的真实内容?

bin_data_file:

0000000 0403 0007 0000 4000 0440 0670 0a8c 0000
0000010 0003 0a00 4946 494e 4153 2052 4f43 5052
0000020 202e 2020 0000 6590 5446 464c 3538 3932
0000030 3350 4e42 2d56 3545 3141 2020 5203 a900
0000040 3a00 0000 5155 3036 3030 2048 2020 2020
0000050 2020 2020 3331 3830 3430 2020 fa68 e505
0000060 0000 0000 0000 0000 0000 0000 0000 0000
*
0000100 005d 00f6 005c 00f8 8890 546f a08c 3c73
0000110 7017 f401 7616 e803 1027 eb04 071f 3106
0000120 e93d c800 2d31 3c01 0000 0000 0000 0000
0000130 0000 0000 0000 0000 0000 0000 0000 0000
0000140 0000 0000 803f 0000 0000 0000 0001 0000
0000150 0001 0000 0001 0000 0001 0000 0000 1f00
0000160 061e 9c81 d70f bf1a 2822 0000 0000 0030
0000170 0000 0000 0000 0000 ffff ffff ffff 01ff
0000180 0040 0078 0b78 3130 2d39 3730 2d38 3430
0000190 0335 3041 0f31 3030 3030 3030 3030 3030
00001a0 3030 3030 0030 0000 0000 0000 0000 0000
00001b0 0000 0000 0000 0000 0000 0000 0000 0000
*
00001f0 0000 0000 6d67 5853 0000 0000 0000 451a
0000200

    //read bin file
   FILE *fp_read = fopen("bin_data_file", "rb");
   if (fp_read == NULL)
     printf("failed to open file.");

   char *buf = malloc(512);
   memset(buf, 0 , 512);
   int ret = fread(buf, 1, 512, fp_read);
   for(i = 0; i<512;i++) {
     printf("buf is %02x\n", buf[i]); //has issue??
   }
   fclose(fp_read);

输出:

03 04 07 00 00 00 00 40 40 04 70 06 ffffff8c 0a 00 00 03 00 00 0a 46 49 4e 49 53 41 52 20 43 4f 52 50 2e 20 20 20 00 00 ffffff90 65 46 54 4c 46 38 35 32 39 50 33 42 4e 56 2d 45 35 41 31 20 20 03 52 00 ffffffa9 00 3a 00 00 55 51 36 30 30 30 48 20 20 20 20 20 20 20 20 20 31 33 30 38 30 34 20 20 68 fffffffa 05 ffffffe5 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 5d 00 fffffff6 00 5c 00 fffffff8 00 ffffff90 ffffff88 6f 54 ffffff8c ffffffa0 73 3c 17 70 01 fffffff4 16 76 03 ffffffe8 27 10 04 ffffffeb 1f 07 06 31 3d ffffffe9 00 ffffffc8 31 2d 01 3c 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 3f ffffff80 00 00 00 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 00 00 00 1f 1e 06 ffffff81 ffffff9c 0f ffffffd7 1a ffffffbf 22 28 00 00 00 00 30 00 00 00 00 00 00 00 00 00 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff 01 40 00 78 00 78 0b 30 31 39 2d 30 37 38 2d 30 34 35 03 41 30 31 0f 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 67 6d 53 58 00 00 00 00 00 00 1a 45

1 个答案:

答案 0 :(得分:1)

问题是转化之一。更具体地说,是在调用变量参数函数(例如UnsafeCell)时进行的implicit conversions

发生的事情是类型小于printf的值将是promotedint。对于带符号类型(可能为int),编译器将执行符号扩展,因此负值保持负值。

您看到的是在two's complement系统上进行这种转换的结果。

要阻止其发生,请改用显式的char类型,例如fixed width integer type unsigned