ManageIQ/binary_struct

Supporting bit decoding and encoding

Open

#14 opened on Mar 5, 2020

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Ruby (9 forks)auto 404
enhancementgood first issuehelp wantedpinnedsize/m

Repository metrics

Stars
 (5 stars)
PR merge metrics
 (PR metrics pending)

Description

I'd like to specify the following:

BinaryStruct.decode([
  'L', :some_num,
  'b', :flag1,
  'b', :flag2,
  'b', :flag3,
  'L', :another_num,
])

Given that, I'd expect BinaryStruct.decode to extract a number, then on the next byte extract the first 3 bits as booleans stored in :flag1, :flag2, :flag3, then move ahead to the next byte and extract another number.

I'd probably be happy with having to align my definition to byte boundaries, so perhaps...

BinaryStruct.decode([
  'L', :some_num,
  'b', :flag1,
  'b', :flag2,
  'b', :flag3,
  'b5', :unused, # ideally optional
  'L', :another_num,
])

I would expect encode and size to handle similar structures.


Implementation wise, the 'b' identifier reads 1 bit then skips to the next byte, So, when building the decode string it will have to be aware of successive bits and then pack those into a b8.

Contributor guide