scott-griffiths / bitstring

A Python module to help you manage your bits
https://bitstring.readthedocs.io/en/stable/index.html
MIT License
405 stars 68 forks source link

ByteArray with bin attribute #211

Closed AleksanderIkleiw closed 3 years ago

AleksanderIkleiw commented 3 years ago

Hey! I really appreciate your work with this library. Could you please tell me where should I change the code to get bin values 3? In other words. Let's say I provide to BitArray data that will return me with .bin attribute 01. I would like to make it return 000111(3 actual bit). I could do my own function, but that would significantly increase the time complexity. Could you please tell me on which line I should change the yield/return so it would yield/return value 3 times?

scott-griffiths commented 3 years ago

Hi. Sorry for the delay in answering. That's an unusual thing to need. If your binary data isn't too long I'd be tempted to get the binary string out, process this in Python to replicate each character 3 times and then convert back to a BitArray.

bs = BitArray(...)
b = ''.join(3*x for x in bs.bin)
bs3 = BitArray(bin=b)

It's not trivial to mess with how the attribute works internally as it would have a lot of unintended side effects elsewhere. An alternative is to work out what mathematical operations are being performed by that transformation and to do them directly - but that's not obvious to me!