0

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.

Matt
  • 179
  • 1
  • 3
  • 14
  • 1
    This was already answered: http://stackoverflow.com/questions/8928240/convert-binary-string-to-int – Uriziel Jan 03 '14 at 23:13

1 Answers1

1

You can use hex() and int():

>>> 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.

arshajii
  • 127,459
  • 24
  • 238
  • 287