Project

rubybits

0.0
No commit activity in last 3 years
No release in over 3 years
RubyBits simplifies the task of parsing and generating binary strings in particular formats.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0.9
~> 1.3.0
~> 1.8.0
~> 2.14.1
~> 0.8.0
 Project Readme

RubyBits

RubyBits is a library that makes dealing with binary formats easier. In particular, it provides the Structure class, which allows for easy parsing and creation of binary strings according to specific formats. More usage information can be found in the docs or by looking at the specs.

You can install via rubygems with gem install rubybits.

Example:

class NECProjectorFormat < RubyBits::Structure
  unsigned :id1,     8,    "Identification data assigned to each command"
  unsigned :id2,     8,    "Identification data assigned to each command"
  unsigned :p_id,    8,    "Projector ID"
  unsigned :m_code,  4,    "Model code for projector"
  unsigned :len,     12,   "Length of data in bytes"
  variable :data,          "Packet data", :length => :len, :unit => :byte
  unsigned :checksum,8,    "Checksum"

  checksum :checksum do |bytes|
    bytes[0..-2].inject{|sum, byte| sum + byte} & 255
  end
end

NECProjectorFormat.parse(buffer)
  => [[<NECProjectorFormat>, <NECProjectorFormat>], rest]

NECProjectorFormat.new(:id1 => 0x44, :id2 => 2, :p_id => 0, :m_code => 0, :len => 5, :data => "hello").to_s.bytes.to_a
  => [0x44, 0x2, 0x05, 0x00, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x5F]