Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert two character string to its hex value in Python

Tags:

python

string

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

I have tried hex("0d")

like image 445
Milind Dumbare Avatar asked May 16 '26 00:05

Milind Dumbare


1 Answers

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'
like image 135
Bhargav Rao Avatar answered May 18 '26 12:05

Bhargav Rao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!