Closed taylorh140 closed 1 month ago
Hi, thanks for your question.
The unpack
method in bitstring does support struct-like format strings. I'm not sure quite what your example unpack string is trying to do, but in bitstring you can either give a list of formats or a comma-separated string. So for example
>>> b = Bits(b'some_byte_data_to_unpack')
>>> b.unpack('<4L, 2*bool, pad, u5')
[1701670771, 1954112095, 1633967973, 1952407924, False, True, 11]
Note that for a 4-byte integer it supports l
/ L
but not i
/ I
. To be honest I can't remember why this is (if anything the I
is the more correct one to choose). I'll raise a separate bug to address that issue - for now use L
instead of I
!
I'm unsure if there was more that you were trying to do in your example, so let me know if this is helpful.
Yeah, I'm getting some things figured out now. Some parts are very compatiable with "struct" and i really like the example you gave.
there are some other oddities too or blanks:
Now that i see how to use it a bit better. The gray'd out stuff is just regex to help readablity. let me know if i got anything wrong.
And also just to be transparent I am updating a 4 byte type code system. and i thought it would be nice for it to be compatible with python. they were enumerated constants but i though i could get most thing to fit into a 4 character string. and it mostly does apart from a few exceptions like "pad24" and "bytes32" or even "bool8" which is so very close. So its nice to see that most of the sort hand matches struct, especially if "L" is used. and i probably could have done if the bitstring short hand had a one character code for each of the items.
I can always make a few special substitutions on my side: p -> Pad, y -> bYtes t -> biTs
"b" is super popular here.
anyhow thanks for your timely reply.
Ill add my wishlist here:
Also i noticed something weird, when I am trying to define a structure i usually do it in terms of a bytefield which defines things from lsb to msb:
As shown above.
but its almost like the library is biased toward starting at the bottom right and working backwards. I think i had to reverse order of all my types to get the expected information.
update:
I think I'm actually having an issue with the lsb0 option. I thought it only affect the bit order inside a byte range. but it also affects the byte order. which is not optimal.
I'm going to go ahead and close this. but consider the short string versions. Thanks!
I have seen this library several time and considered using it but i always end up not doing it due to my preceived incompaitiblity with the "struct" library.
I guess what i would like to see or find is a extension to the struct that keeps backwards compatiblity but adds the option of doing bit (un)packing.
so i could for example:
mix and match the struct items but also expand the bits when needed, instead of having a second set of operations after I break out the first sets of data.
It might be possible to do this already, but I just haven't seen any examples.