How to create an array of bits.

87 views
Skip to first unread message

Binyamin Sharet

unread,
Jun 26, 2015, 6:12:14 PM6/26/15
to const...@googlegroups.com
Hi,

Is there a way to define an a bit array as part of a standard struct?

I successfully created it as a BitStruct:

>>> c = BitStruct('bs', Array(96, Bit('b')))
>>> c.parse('aa55aa55ffff0000aa55aa55'.decode('hex'))

Container({'b': [1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1]})

But when trying to embed it in a struct, I get an exception (SizeofError):

>>> d = Struct('s', EmbeddedBitStruct(BitStruct('bs', Array(96, Bit('b')))))
>>> d.parse('aa55aa55ffff0000aa55aa55'.decode('hex'))

---------------------------------------------------------------------------
SizeofError                               Traceback (most recent call last)
<ipython-input-69-c863f7ae7f02> in <module>()
----> 1 d.parse('aa55aa55ffff0000aa55aa55'.decode('hex'))
<pythonpath>/site-packages/construct/core.pyc in parse(self, data)
--> 188         return self.parse_stream(BytesIO(data))
<pythonpath>/site-packages/construct/core.pyc in parse_stream(self, stream)
--> 198         return self._parse(stream, Container())
<pythonpath>/site-packages/construct/core.pyc in _parse(self, stream, context)
--> 668                 sc._parse(stream, context)
<pythonpath>/site-packages/construct/core.pyc in _parse(self, stream, context)
-> 1094         data = _read_stream(stream, self._sizeof(context))
<pythonpath>/site-packages/construct/core.pyc in _sizeof(self, context)
-> 1105         return self.resizer(self.subcon._sizeof(context))
<pythonpath>/site-packages/construct/macros.pyc in resizer(length)
--> 354             raise SizeofError("size must be a multiple of 8", length)
SizeofError: ('size must be a multiple of 8', 12)

Is there a standard way of doing that?

Thanks,
Binyamin

Frank Carney

unread,
Jun 30, 2015, 3:50:54 PM6/30/15
to const...@googlegroups.com
That is because decode("hex") is not converting to a number representation of your hex string.  It is converting to a string (text) that is represented by the hex.  So it may be generating unicode characters in the resulting string which is almost certainly not going to be bit aligned to 8 bits. 
Instead do this bytearray.fromhex('aa55aa55ffff0000aa55aa55')  (https://docs.python.org/3.1/library/functions.html#bytearray).  bytearrays are great for manipulating binary data in Python, if you are not aware of that.
...

Frank Carney

unread,
Jun 30, 2015, 3:57:48 PM6/30/15
to const...@googlegroups.com
Also my solution is wrong too.  There is only half the data needed.  As each hex number is 4 bits.  So you need another 16 hex numbers or 8 pairs to make up a total of 96 bytes. So you need 32 actual hex digits in your number. 

Arek Bulski

unread,
Sep 26, 2016, 10:54:20 AM9/26/16
to Construct
Hold your horses. First of all, Embedded() only allows to embed structs in structs and sequences in sequences. Range is not even embeddable, much less in a dictionary. 

You can use Bitwise(Array(...)) no problem.

Arek Bulski

unread,
Sep 26, 2016, 11:02:28 AM9/26/16
to Construct
Ah got that wrong, I appologize. The issue is that EmbeddedBitStruct(BitStruct is wrong. You should not put Bitwise inside another Bitwise which is the case here. Put normal structs inside.
Reply all
Reply to author
Forward
0 new messages