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

Ivan Kozlov picture Ivan Kozlov · May 29, 2013 · Viewed 28.4k times · Source

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?

Answer

M. Shiina picture M. Shiina · May 30, 2013

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.