我正在尝试读取一个二进制文件并将其中的每个字节打印为 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/