Ruby使用pack和unpack写入并读入二进制文件

时间:2016-02-23 23:59:34

标签: ruby binary pack unpack

我正在为二进制文件进行基本的Ruby读/写操作。

这是我的代码

UAirship.push().pushNotificationDelegate = customPushDelegate

到目前为止一切看起来都不错。然后我尝试从文件中读取并将其转回ASCII格式。

afile = File.new("test.bin",'wb')

test = "abc"
a = test.unpack("B*")

File.open("test.bin", 'wb' ) do |output|
  output.write a
end

print ("write #{a}\n") #Output "write ["011000010110001001100011"]

b = a.pack("B*")
print ("b is #{b}\n") #Output "b is abc"

然后出现错误。

" extract.ruby:23:s = File.binread("test.bin") bits = s.pack("B*") print ("s is #{s}\n") #Output is "s is ["011000010110001001100011"]" print ("bits is #{bits}\n") 包' for" [\" 011000010110001001100011 \"]":String(NoMethodError)"

有人能帮我解释一下吗?非常感谢!

0 个答案:

没有答案