3

I want to convert "0d" to 0xd or "ff" to 0xff.

I have tried hex("0d")

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Milind Dumbare
  • 3,104
  • 2
  • 19
  • 32
  • possible duplicate of [Convert hex string to int in Python](http://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python) – AncientSwordRage Feb 12 '15 at 15:54

1 Answers1

6

Do base conversion as 16, the second operand to int takes the base value of the number you need to convert

>>> a = "0d"
>>> int(a,16)
13
>>> hex(int(a,16))
'0xd'
>>> a = "ff"
>>> int(a,16)
255
>>> hex(int(a,16))
'0xff'
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140