I want to convert binary literal like 0b01011 into the binary string like 0101101101 . How i should go in here.
here suppose A=81 (hex)
>>> a = bin(int(str('A'),16))
>>> print a
returns 0b1010 and i want like 10000001 (binary string)
With python's newer string handling:
The '08' specifies an output size of 8, padded with zeros, so this this, by default, shows a whole byte. Without the zero, it is padded with spaces:
Without the '8', the output string is only as large as needed:
|
|||||
|
I don't really get what you're asking, but you can just do what you're already doing and chop off the
When you enter a binary literal, it turns into a number. The number doesn't depend on its representation in binary or any other base; it's just stored as an integer value. So it doesn't matter whether you input it as a binary literal or not, you can still use |
|||||||||
|
You need to use the
If you don't want the
|
|||||||||||||||||||||
|
As far as I understand (based on comments) you get the number as an integer:
and then you want to interpret it as hexadecimal. In order to do that cast it to a string (note that I'm casting a variable
then cast it to int base 16:
finally convert it to a binary representation:
and take all characters from second position (ommit first two):
|
|||||||||||||
|
1010110
from the number ten (or the number one hundred twenty-nine)? If you just don't want the0b
at the front, you can chop it off witha[2:]
. – BrenBarn Jan 1 at 19:44format(81, 'b')
. – Ashwini Chaudhary Jan 1 at 19:44bin(int(str(A),16))[2:]
(note the lack of''
) – freakish Jan 1 at 19:52