How to convert hex string to hex number?

Viewed 42684

I have integer number in ex. 16 and i am trying to convert this number to a hex number. I tried to achieve this by using hex function but whenever you provide a integer number to the hex function it returns string representation of hex number,

my_number = 16
hex_no = hex(my_number)    
print type(hex_no) // It will print type of hex_no as str.

Can someone please tell me how to convert hex number in string format to simply a hex number.
Thanks!!

8 Answers

I think here most of the answers were misinterpreted or they understood the question wrongly.

To answer to your question it is IMPOSSIBLE to convert resultant string representation of Hex data to Hex numbers(integer representation).

Because, when you convert an integer to hex by doing following

>>> a = hex(34)
>>> print type(a)
<type 'str'>
>>> print a
0x22
>>> a
'0x22'

And some answers were confused here,

>>> print a
0x22
>>> a
'0x22'

When you type print a in interpreter it will result the string data WITHOUT quotes and If you simply type the variable name without using print statement then it would print the string data WITH single/double quotes.

Though the resultant value is Hex data but the representation is in STRING.

As per Python docs you cannot convert to Hex number as I told earlier.

Thanks.

Related