BinaryStruct is a class for dealing with binary structured data. It simplifies expressing what the binary structure looks like, with the ability to name the parts. Given this definition, it is easy to encode/decode the binary structure from/to a Hash.
As an example, we will show reading and writing a .gif header. This example is also in spec/gif_spec.rb.
gif_header = BinaryStruct.new([
"a3", :magic,
"a3", :version,
"S", :width,
"S", :height,
"a", :flags,
"C", :bg_color_index,
"C", :pixel_aspect_ratio
])
header_size = gif_header.size
header = File.open("test.gif", "rb") { |f| f.read(header_size) }
gif_header.decode(header)
=> {
:magic => "GIF",
:version => "89a",
:width => 16,
:height => 16,
:flags => "\x80",
:bg_color_index => 0,
:pixel_aspect_ratio => 0
}
header = gif_header.encode({
:magic => "GIF",
:version => "89a",
:width => 16,
:height => 16,
:flags => "\x80",
:bg_color_index => 0,
:pixel_aspect_ratio => 0
})
File.open("test.gif", "wb") { |f| f.write(header) }
=> "GIF89a\x10\x00\x10\x00\x80\x00\x00"
Add this line to your application's Gemfile:
gem 'binary_struct'
And then execute:
$ bundle
Or install it yourself as:
$ gem install binary_struct
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request