I have a binary value that is stored in a variable. How do I convert it to decimal value or hex value??
t = '0b'+bin(0o202)[::-1][:-2]
So the value of the t would be
t = '0b01000001'
I need the value of the t converted to decimal or hex.
-
1This was already answered: stackoverflow.com/questions/8928240/…Uriziel– Uriziel2014年01月03日 23:13:30 +00:00Commented Jan 3, 2014 at 23:13
1 Answer 1
>>> hex(int(t, 2)) # hex
'0x41'
>>>
>>> str(int(t, 2)) # decimal
'65'
Note that integers are by default represented in decimal, which is why the last line works as it should.
answered Jan 3, 2014 at 23:12
arshajii
130k26 gold badges246 silver badges293 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py