Ruby - 从文件读取字节,转换为整数
我正在尝试从文件中读取无符号整数(存储为连续字节)并将它们转换为整数。我试过这个:
file = File.new(filename,"r")
num = file.read(2).unpack("S") #read an unsigned short
puts num #value will be less than expected
我在这里做错了什么?
I'm trying to read unsigned integers from a file (stored as consecutive byte) and convert them to Integers. I've tried this:
file = File.new(filename,"r")
num = file.read(2).unpack("S") #read an unsigned short
puts num #value will be less than expected
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您没有读取足够的字节。正如您在对 tadman 的回答的评论中所说,您得到的是
202
而不是3405691582
请注意,
0xCAFEBABE
的前 2 个字节是0xCA
=202
如果您确实想要在一个数字中包含所有 8 个字节,那么您需要读取的内容比 unsigned Short 还要多
。
下划线假设本机 long 将是 8 个字节,这绝对不能保证。
You're not reading enough bytes. As you say in the comment to tadman's answer, you get
202
instead of3405691582
Notice that the first 2 bytes of
0xCAFEBABE
is0xCA
=202
If you really want all 8 bytes in a single number, then you need to read more than the unsigned short
try
The underscore is assuming that the native long is going to be 8 bytes, which definitely is not guaranteed.
看看《镐》怎么样? (Ruby 1.9,第 44 页)
each_byte 逐字节迭代文件。
How about looking in The Pickaxe? (Ruby 1.9, p. 44)
each_byte iterates over a file byte by byte.
有几个库可以帮助您在 Ruby 中解析二进制数据,让您在简单的高级声明性 DSL 中声明数据格式,然后通过以下方式计算出所有打包、解包、位旋转、移位和字节序转换:他们自己。
我从未使用过其中之一,但这里有两个例子。 (还有更多,但我不知道):
There are a couple of libraries that help with parsing binary data in Ruby, by letting you declare the data format in a simple high-level declarative DSL and then figure out all the packing, unpacking, bit-twiddling, shifting and endian-conversions by themselves.
I have never used one of these, but here's two examples. (There are more, but I don't know them):
好的,我开始工作了:
感谢您的帮助。
Ok, I got it to work:
Thanks for all of your help.
文件中存储的数字是什么格式?是十六进制吗?你的代码对我来说看起来是正确的。
What format are the numbers stored in the file? Is it in hex? Your code looks correct to me.
如果您使用的是 Windows,则在处理二进制数据时,您需要确保以二进制模式打开文件。这对于阅读和写作都适用。
根据源平台的不同,“endian”编码也可能存在问题。例如,基于 PowerPC 的计算机,包括旧的 Mac 系统、IBM Power 服务器、PS3 集群或 Sun Sparc 服务器。
你能举个例子来说明它是如何“少”的吗?通常数据有明显的模式。
例如,如果您想要 0x1234,但得到 0x3412,则这是字节序问题。
When dealing with binary data you need to be sure you're opening the file in binary mode if you're on Windows. This goes for both reading and writing.
There may also be issues with "endian" encoding depending on the source platform. For instance, PowerPC-based machines, which include old Mac systems, IBM Power servers, PS3 clusters, or Sun Sparc servers.
Can you post an example of how it's "less"? Usually there's an obvious pattern to the data.
For example, if you want 0x1234 but you get 0x3412 it's an endian problem.