{"id":18103161,"url":"https://github.com/manageiq/binary_struct","last_synced_at":"2025-04-13T19:09:22.198Z","repository":{"id":8072499,"uuid":"9485306","full_name":"ManageIQ/binary_struct","owner":"ManageIQ","description":"BinaryStruct is a class for dealing with binary structured data.","archived":false,"fork":false,"pushed_at":"2024-04-26T20:55:49.000Z","size":64,"stargazers_count":4,"open_issues_count":4,"forks_count":11,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-04-26T21:46:07.769Z","etag":null,"topics":["ruby"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ManageIQ.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-04-16T23:50:41.000Z","updated_at":"2024-06-12T21:51:28.238Z","dependencies_parsed_at":"2024-04-26T21:41:37.588Z","dependency_job_id":"d1f403c4-4e5b-4359-a46b-1547f412b3e6","html_url":"https://github.com/ManageIQ/binary_struct","commit_stats":{"total_commits":47,"total_committers":8,"mean_commits":5.875,"dds":"0.36170212765957444","last_synced_commit":"de470c55d2a1e98015406fd08b150cf41f8978a2"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ManageIQ%2Fbinary_struct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ManageIQ%2Fbinary_struct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ManageIQ%2Fbinary_struct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ManageIQ%2Fbinary_struct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ManageIQ","download_url":"https://codeload.github.com/ManageIQ/binary_struct/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246178312,"owners_count":20736122,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ruby"],"created_at":"2024-10-31T22:10:42.998Z","updated_at":"2025-03-29T11:30:54.173Z","avatar_url":"https://github.com/ManageIQ.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BinaryStruct\n\n[![Gem Version](https://badge.fury.io/rb/binary_struct.svg)](http://badge.fury.io/rb/binary_struct)\n[![CI](https://github.com/ManageIQ/binary_struct/actions/workflows/ci.yaml/badge.svg)](https://github.com/ManageIQ/binary_struct/actions/workflows/ci.yaml)\n[![Maintainability](https://api.codeclimate.com/v1/badges/98184649fd8158e259d8/maintainability)](https://codeclimate.com/github/ManageIQ/binary_struct/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/98184649fd8158e259d8/test_coverage)](https://codeclimate.com/github/ManageIQ/binary_struct/test_coverage)\n\nBinaryStruct is a class for dealing with binary structured data.  It simplifies\nexpressing what the binary structure looks like, with the ability to name the\nparts.  Given this definition, it is easy to encode/decode the binary structure\nfrom/to a Hash.\n\n## Example Usage\n\nAs an example, we will show reading and writing a .gif header.  This example is\nalso in spec/gif_spec.rb.\n\n### Create the structure definition\n\n```ruby\ngif_header = BinaryStruct.new([\n  \"a3\", :magic,\n  \"a3\", :version,\n  \"S\",  :width,\n  \"S\",  :height,\n  \"a\",  :flags,\n  \"C\",  :bg_color_index,\n  \"C\",  :pixel_aspect_ratio\n])\n```\n\n### Read the header\n\n```ruby\nheader_size = gif_header.size\nheader = File.open(\"test.gif\", \"rb\") { |f| f.read(header_size) }\ngif_header.decode(header)\n\n=\u003e {\n  :magic              =\u003e \"GIF\",\n  :version            =\u003e \"89a\",\n  :width              =\u003e 16,\n  :height             =\u003e 16,\n  :flags              =\u003e \"\\x80\",\n  :bg_color_index     =\u003e 0,\n  :pixel_aspect_ratio =\u003e 0\n}\n```\n\n### Write the header\n\n```ruby\nheader = gif_header.encode({\n  :magic              =\u003e \"GIF\",\n  :version            =\u003e \"89a\",\n  :width              =\u003e 16,\n  :height             =\u003e 16,\n  :flags              =\u003e \"\\x80\",\n  :bg_color_index     =\u003e 0,\n  :pixel_aspect_ratio =\u003e 0\n})\nFile.open(\"test.gif\", \"wb\") { |f| f.write(header) }\n\n=\u003e \"GIF89a\\x10\\x00\\x10\\x00\\x80\\x00\\x00\"\n```\n\n## Note about bit and nibble formats\n\nBinaryStruct supports bit formats and nibble formats, however note that the\nunderlying Ruby methods, [pack](http://ruby-doc.org/core-2.2.0/Array.html#method-i-pack)\nand [unpack](http://ruby-doc.org/core-2.2.0/String.html#method-i-unpack), support\nless than 8 bits by reading an entire byte, even if all of the bits are not used.\n\nFor example,\n\n```ruby\ns = \"\\xFF\\x00\"       # binary: 1111111100000000\ns.unpack(\"b8b8\")     # =\u003e [\"11111111\", \"00000000\"]\ns.unpack(\"b4b4b4b4\") # =\u003e [\"1111\", \"0000\", \"\", \"\"]\n```\n\nOne might expect that the latter would read 4 bits, then the next 4 bits, etc,\nyielding `[\"1111\", \"1111\", \"0000\", \"0000\"]`, but that is not the case.  Instead,\nthe first b4 reads a full byte's worth, then discards the unused 4 bits, and the\nsame happens for the next b4.  The third and fourth b4 have nothing left to read,\nand so just return empty strings.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'binary_struct'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install binary_struct\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanageiq%2Fbinary_struct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmanageiq%2Fbinary_struct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanageiq%2Fbinary_struct/lists"}