JOKER
JOKER

Reputation: 75

Format entries in list to 8 bit binary in python

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

Answers (1)

blhsing
blhsing

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

Related Questions