Sonntag, 5. April 2009

Reading Binary Files with Ruby

For my current project I need to read some binary data with Ruby. The data file is formatted as 16-bit BINARY INTEGERS, hi-byte-first order with 2160 x 4320 data values.

From my Java days I knew the DataInputStream class. With it it's every easy to read the data, just use io.readShort().
Therefore I was looking everywhere in IO and File for some methods to read the data in an appropriate format. But I couldn't find anything really useful as all methods are working with strings and characters.

But then I found the pack method in Array and its corresponding method unpack in String. With this two methods it's easy to read and write binary files in various formats.

Here the code snippet where I read the gzip compressed file. @size is in my case 4320 and @height an array of 2160. After reading the file I can access the data with @data[line][row]. do |gz|
  x = 0
  # read one line (16-Bit-Integer = 2 bytes)
  while line = * @size) 
    @data[x] = line.unpack('s' * @size)
    x += 1
If you need more, have a look at BitStruct. It provides a nice DSL for handling binaries.

Keine Kommentare: