【发布时间】:2012-04-02 10:45:41
【问题描述】:
我正在尝试转换 4 字节数组的解压值?这在 Ruby 中可行吗?
假设我写了 b1 = b.unpack("N") 并打印了 b1 的值,即 1 。但是当我尝试使用 .to_i 将 b1 转换为某个整数时,控制台会抛出错误 test.rb:13: undefined methodto_i' for [118]:Array (NoMethodError)`
我的代码如下:
File.open('testfile','rb') do |file|
file.read.scan(/(.{4})(.{4})(.{4})(.*\w)(.{8})/).each do |a,b,c,d,e|
if a == "cook"
puts "test1"
else
puts "test2"
end
puts "output1"
b1 = b.unpack("N")
puts "output2"
c1 = c.unpack("N")
puts "output3"
puts "output4"
puts "output5"
end
end
【问题讨论】:
-
b1是一个数组;to_i是什么意思?如果你想要它作为一个整数,你需要使用b1[0]。