Closed jbhannon1 closed 2 months ago
Hi there.
The issue is that the tobytes
method may need to pad with between 0 and 7 bits in order to return a whole number of bytes.
Your a0
array has 10 unsigned integers, each of 5 bits, so that's 50 bits in total. When this is converted to bytes it needs to add 6 bits on the end to make it a whole number of bytes. When this is used to create a new array you have enough data for 11 u5
elements, plus one spare bit.
>>> a0
Array('uint5', [31, 31, 31, 31, 31, 31, 31, 31, 31, 31])
>>> a1
Array('uint5', [31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 0], trailing_bits=BitArray('0b0'))
So basically it's because you've explicitly asked for the data to be stored in a bytes
object as an intermediary, and this can't be represented without some padding or losing some bits. The array has a data
member that stores the bits, so that can be used instead, but it depends what you're trying to do.
a2 = Array('u5', a0.data)
Thanks very much for the quick response. I will keep track of the length and not rely on the length of the bytes object to give me the number of bits. Sent from my iPhoneOn May 29, 2024, at 10:41 AM, Scott Griffiths @.***> wrote: Hi there. The issue is that the tobytes method may need to pad with between 0 and 7 bits in order to return a whole number of bytes. Your a0 array has 10 unsigned integers, each of 5 bits, so that's 50 bits in total. When this is converted to bytes it needs to add 6 bits on the end to make it a whole number of bytes. When this is used to create a new array you have enough data for 11 u5 elements, plus one spare bit.
a0 Array('uint5', [31, 31, 31, 31, 31, 31, 31, 31, 31, 31]) a1 Array('uint5', [31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 0], trailing_bits=BitArray('0b0'))
So basically it's because you've explicitly asked for the data to be stored in a bytes object as an intermediary, and this can't be represented without some padding or losing some bits. The array has a data member that stores the bits, so that can be used instead, but it depends what you're trying to do. a2 = Array('u5', a0.data)
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>
If I convert an Array using .tobytes(), and then initialize a new Array from those bytes, the length can be different. why?
here is a minimal example
the output is