Reputation: 75
I have a list A = [100011, 1110110]
. I want to make each entry in the list 8 bit using the format option:
B = '{0:08b}'.format(A[1])
B = '{0:08b}'.format(A[2])
But when I print(B)
. I am getting the output as: B = 100001111000001011110
and B = 11000011010101011
Why is this happening? Is this command not used for list?
Upvotes: 1
Views: 659
Reputation: 106598
The integer literals defined in the list A
are specified in decimals. You should specify them as binary literals (prefixed with 0b
) instead:
A = [0b100011, 0b1110110]
print('{0:08b}'.format(A[0]))
print('{0:08b}'.format(A[1]))
This outputs:
00100011
01110110
Upvotes: 2