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.

asked Jan 3, 2014 at 23:09
1

1 Answer 1

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.

answered Jan 3, 2014 at 23:12
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.