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

标签 c binary hex

我正在尝试读取一个二进制文件并将其中的每个字节打印为 2hex 数字,但是当 当我的程序应该是“20”时,我到达了一个空间,但它却打印了“0”,并且每 20 就会执行一次此操作

我的输出0 17 FF FF FF FF 0 0 17 FF FF FF FF FF 0 0 0 FF 0 0

来自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

有人知道这是怎么回事吗? 文件 = ÿÿÿÿ ÿÿÿÿÿ ÿ

//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套。

关于c - 在 C 中读取二进制文件错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21959577/

相关文章:

c# - .NET 二进制文件读取性能

perl - 在 Perl 中获取十六进制字符串的 SHA1 HMAC

c - 如何清空 GLib 树?

c - 键入 8 位数字后,使用 scanf 的 While 循环会导致堆栈溢出

java - 我们如何部署二进制依赖项?

php - 三重十六进制代码转RGB

java - Java 十六进制字符串转换为字节

c - 关于将 char[] 传递给 isdigit()

c - 将 void 指针作为函数参数传递以获取新地址(char、int)

c - 空终止符是否适合解析二进制文件