Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write array of radix-2 numeric strings to binary file in Ruby

Tags:

io

ruby

bindata

I've written a simple Huffman encoding in Ruby. As output I've got an array, for example:

["010", "1111", "10", "10", "110", "1110", "001", "110", "000", "10", "011"]

I need to write, and then read, it to and from a file. I tried several methods:

IO.binwrite("out.cake", array)

I get a simple text file and not binary.

Or:

File.open("out.cake", 'wb' ) do |output|
  array.each do | byte |
       output.print byte.chr
  end
end

Which looks like it works, but then I can't read it into array.

Which encoding should I use?

like image 932
Ivan Kozlov Avatar asked May 29 '13 18:05

Ivan Kozlov


1 Answers

I think you can just use Array#pack and String#unpack like the following code:

# Writing
a = ["010", "1111", "10", "10", "110", "1110", "001", "110", "000", "10", "011"]
File.open("out.cake", 'wb' ) do |output|
  output.write [a.join].pack("B*")
end

# Reading
s = File.binread("out.cake")
bits = s.unpack("B*")[0] # "01011111010110111000111000010011"

I don't know your preferred format for the result of reading and I know the above method is inefficient. But anyway you can take "0" or "1" sequentially from the result of unpack to traverse your Huffman tree.

like image 88
M. Shiina Avatar answered Oct 06 '22 10:10

M. Shiina