字符阵列中的字节如何代表整数?
因此,假设我有从二进制文件中读取的char数组(例如Ext2格式的文件系统图像文件)。
现在,我需要阅读从Offset Byte 1024(< - 这是数据开始的偏移)开始的整数。是否有任何整洁的方法。整数可能是任何数字。因此,我相信我的系统上可以以4个字节的整数大小表示(x86-64)。
我相信我需要使用strtol,例如:
/* Convert the provided value to a decimal long */
char *eptr=malloc(4);// 4 bytes becuase sizeof int is 4 bytes
....
int valread=read(fd,eptr,4);//fd is to ext2 formatted image file (from file system)
result = strtol(eptr, &v, 10);
以上是long
,所以这是代表整数32位的数字吗?
EPTR
是否应该终止?
这是正确的吗?
So let's say I have char array that I read from binary file (like ext2 formatted filesystem image file).
Now I need to read integer starting at offset byte 1024(<--that's the offset from start of data). Is there any neat way of doing it. The integer could be any number. So I believe can be represented in integer size of 4 byte on my system (x86-64).
I believe I need to use strtol like:
/* Convert the provided value to a decimal long */
char *eptr=malloc(4);// 4 bytes becuase sizeof int is 4 bytes
....
int valread=read(fd,eptr,4);//fd is to ext2 formatted image file (from file system)
result = strtol(eptr, &v, 10);
The above is long
so is this the number to represent a integer 32 bit?
Should eptr
be null terminated?
Is this correct or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以二进制模式打开文件
而不是使用
int
,这可能与4个字样不同,请考虑&lt; stdint.h&gt;
。帐户 endian 。
因为文件数据为 litty-endian> little-endian ,转换为本地endian。参见
#include&lt&lt; endian.h&gt;
。
完成后清理
strtol()
检查A String 并返回long
。文件数据是二进制的,而不是字符串。Open the file in binary mode
Rather than use
int
, which may differ from 4-bytes, considerint32_t
from<stdint.h>
.Account for Endian.
As file data is little-endian, convert to native endian. See
#include <endian.h>
.Clean up when done
No.
strtol()
examines a string and returns along
. File data is binary and not a string.在
strtol
的情况下,看到一些代码可能更容易遵循。 ,这里非常简化的strtol
一种函数:如果我们使用字符串
“ 123”
调用它因此 字符串null末端和循环以
value
等于int
value123
等于。我希望这使得字符串转换如何工作更清楚。
虽然以上是字符串,但如果您读取现有
int
值的原始二进制位,则不应调用strtol
,因为数据不是字符串。相反,您基本上将四个字节解释为一个32位值。
不幸的是,在不知道关于 。
endianness是命令字节组成整数值的方式。服用(十六进制)编号
0x01020304
可以将它们存储为0x01
,0x02
,0x03
和0x04
(这称为big-endian); 或作为0x04
,0x03
,0x02
和0x01
endian)。在一个小型系统(您的普通PC状系统)上说您有这样的数组:
然后,您可以将其复制到
int
:这将使
int
变量值
等于0x01020304
。In the case of
strtol
it might be easier to follow along by seeing some code. So here a very simplifiedstrtol
kind of function:If we call it with the string
"123"
and unroll the loop what's happening is this:In the fourth iteration we reach the string null-terminator and the loop ends with
value
being equal to theint
value123
.I hope this makes it a little clearer about how string to number conversions are working.
While the above is for strings, if you read the raw binary bits of an existing
int
value, then you should not callstrtol
because the data isn't a string.Instead you basically interpret the four bytes as a single 32-bit value.
Unfortunately it's not easy to explain how these bits are interpreted without knowing a thing or two about endianness.
Endianness is how the bytes are ordered to make up the integer value. Taking the (hexadecimal) number
0x01020304
they can be stored either as0x01
,0x02
,0x03
and0x04
(this is called big-endian); Or as0x04
,0x03
,0x02
and0x01
(this is called little-endian).On a little-endian system (your normal PC-like system) say you have an array like this:
then you could copy it into an
int
:and that will make the
int
variablevalue
equal to0x01020304
.