Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitSequence Bug by using bits with padded zeros #317

Open
bauergeorg opened this issue Sep 7, 2022 · 1 comment
Open

BitSequence Bug by using bits with padded zeros #317

bauergeorg opened this issue Sep 7, 2022 · 1 comment

Comments

@bauergeorg
Copy link

Hi I found the following bug:

from pyftdi.bits import BitSequence

value1 = '002' # hex
length = 3*4

print(value1)
int1 = int(value1,16)
print(int1)
bin1 = bin(int1)
print(bin1)
print(bin1[2:])
ans1 = BitSequence(bin1[2:], msb=True, length=length)
print(ans1)

value2 = '200' # hex
length = 3*4

print(value2)
int2 = int(value2,16)
print(int2)
bin2 = bin(int2)
print(bin2)
print(bin2[2:])
ans2 = BitSequence(bin2[2:], msb=True, length=length)
print(ans2)

The output is a litte bit confusing...

002
2
0b10
10
12: 1000 00000000
200
512
0b1000000000
1000000000
12: 1000 00000000

I think this is the same issue like: #245

@bauergeorg
Copy link
Author

I found a solution to convert a hex value into a binary with padded zeros:

def hex_to_bits(value:str, length=None, padded_zeros=True) -> str:
    if length == None:
        length = len(value)*4
    ret = bin(int(value,16))[2:]
    if padded_zeros == True:
        ret = ret.rjust(length, '0')
    return ret

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant