gpt4 book ai didi

c - 从 C 中的 bin 文件中读取signed int 给出了错误的结果

转载 作者:行者123 更新时间:2023-11-30 15:48:51 25 4
gpt4 key购买 nike

我想从 .bin 文件读取数据。实际上,如果我预览 bin 文件的数据,我会看到如下内容:

0000000    3030    3030    3030    3730    300a    3030    3030    3030
0000010 0a35 3330 3030 3030 3030 300a 3031 3030

所以我只想先读取前 2 个 32 位有符号整数

我的代码是这样的:

int data,data2;

fread(&data,4,1, ptr_myfile);
printf("First Data read in hex is: %x\n",data);

/*read the second 32 bit integer*/
fread(&data2,sizeof(int),1, ptr_myfile);
printf("Second data read in hex is: %x\n",data2);

我的输出是这样的:

First Data read in hex is: 30303030
Second data read in hex is: 37303030
  1. 所以我的第一个问题是为什么要按这个顺序阅读它们?第二个不是30303730?假设我必须读取前两个有符号 32 位整数,哪一个是正确的?

还有更重要的

  1. 第二个数字声明 bin 文件中应存在的其余 32 位有符号整数。有一些注释描述了这个 bin 文件,我知道第二个数字应该等于 4,或者稍大一点,但无论如何都不是 37303030,这是一个非常大的数字。

我认为我的转换或读取 bin 文件的方式有问题。

bin 文件应该包含:

编辑:

The bin file is ASCII text with UNIX-style line-endings. It
consist of a series of 32-bit signed integers in hexadecimal only

对我在这里缺少什么有什么帮助吗?

最佳答案

比较原始格式的多字节二进制文件时,您可能必须考虑 Endianess 。至少,当您在读取并打印后显示数字字节时,它们显示为“反转”,这是我想到的第一件事。

关于c - 从 C 中的 bin 文件中读取signed int 给出了错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16618774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com