BinData - Parsing Binary Data in Ruby
Latest commit 54c4c01 Apr 9, 2017 @dmendel Bump version number

What is BinData?

Version     Travis CI   Quality     Coverage

Do you ever find yourself writing code like this?

io =
len ="v")
name =
width, height ="VV")
puts "Rectangle #{name} is #{width} x #{height}"

It’s ugly, violates DRY and feels like you’re writing Perl, not Ruby.

There is a better way. 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

io =
r  =
puts "Rectangle #{} is #{r.width} x #{r.height}"

BinData provides a declarative way to read and write structured binary data.

This means the programmer specifies what the format of the binary data is, and BinData works out how to read and write data in this format. It is an easier (and more readable) alternative to ruby's #pack and #unpack methods.

BinData makes it easy to create new data types. It supports all the common primitive datatypes that are found in structured binary data formats. Support for dependent and variable length fields is built in.


$ gem install bindata

or if running ruby 1.8

$ gem install bindata -v '~> 1.8.0'


Read the wiki.


If you have any queries / bug reports / suggestions, please contact me (Dion Mendel) via email at