Posted By: Dion Mendel
Date: 2009-08-31 05:45
Summary: BinData 0.11.1
Project: BinData
= BinData is a declarative way to read and write structured binary data.
This is a bug fix release. BinData no longer uses new ruby features introduced in 1.8.7, to keep compatibility with 1.8.6.
This release includes a draft tutorial which will eventually replace the existing minimal documentation.
Release 1.0 is just around the corner.
== What is BinData?
Do you ever find yourself writing code like this?
io = File.open(...)
len = io.read(2).unpack("v")
name = io.read(len)
width, height = io.read(8).unpack("VV")
puts "Rectangle #{name} is #{width} x #{height}"
It's ugly, violates DRY and feels like you're writing Perl, not Ruby.
Here's how you'd write the above using BinData.
class Rectangle < BinData::Record
. endian :little
. uint16 :len
. string :name, :read_length => :len
. uint32 :width
. uint32 :height
end
io = File.open(...)
r = Rectangle.read(io)
puts "Rectangle #{r.name} is #{r.width} x #{r.height}"
BinData supports signed/unsigned integers, floats, bitstrings, strings, null terminated strings, arrays, choices and user defined structures. |
|