简体   繁体   English

在C错误的输出中读取二进制文件

[英]Reading binary file in C wrong output

I'm trying to read a binary file and print each byte inside as 2hex digits, but when I get to a space my program prints '0' when it should be '20' and it does this for every 20 我正在尝试读取一个二进制文件并将每个字节打印为2hex数字,但是当我到达某个空格时,程序应将其打印为“ 20”时打印出“ 0”,并且每20进行一次打印

my output 0 17 FF FF FF FF 0 0 17 FF FF FF FF FF 0 0 0 FF 0 0 我的输出0 17 FF FF FF FF 0 0 17 FF FF FF FF FF 0 0 0 FF 0 0

output from http://www.percederberg.net/tools/text_converter.html 来自http://www.percederberg.net/tools/text_converter.html的输出

20 17 FF FF FF FF 20 20 17 FF FF FF FF FF 20 20 20 FF 20 20

does anyone know whats going on? 有人知道发生了什么吗? file = ÿÿÿÿ ÿÿÿÿÿ ÿ 文件= ÿÿÿÿ ÿÿÿÿÿ ÿ

//Read the test.bin file!!
#include<stdio.h>
   char initial[] = "test.bin";
struct rec{
   unsigned char mydata;
};
int readfile(char []){
   int counter;
   FILE *ptr_myfile;
   struct rec my_record;
   ptr_myfile=fopen(initial,"r");
   if (!ptr_myfile){
      printf("Unable to open file!");
      return 1;
   }
   for ( counter=1; counter <= 20; counter++){
      fread(&my_record,sizeof(struct rec),1,ptr_myfile);
      printf("%X\n",my_record.mydata);      
   }
   fclose(ptr_myfile);
   return 0;
}
int main(){
   readfile(initial);
   return 0;
}

该代码可以正常工作,但是在将二进制数据复制到网站中时, 0x00字节最终变成了空格,这些空格的ASCII值和兼容字符集的值为0x20

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

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