Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert hex string to hex number?

Tags:

python

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

like image 254
Rise Avatar asked Oct 11 '10 07:10

Rise


People also ask

How can I convert a hex string to an integer value?

To convert a hexadecimal string to a numberUse the ToInt32(String, Int32) method to convert the number expressed in base-16 to an integer. The first argument of the ToInt32(String, Int32) method is the string to convert. The second argument describes what base the number is expressed in; hexadecimal is base 16.

How do I encode a hex string?

Hex encoding is performed by converting the 8 bit data to 2 hex characters. The hex characters are then stored as the two byte string representation of the characters. Often, some kind of separator is used to make the encoded data easier for human reading.

How do you parse a hex string in Python?

Use base=16 as a second argument of the int() function to specify that the given string is a hex number. The int() function will then convert the hex string to an integer with base 10 and return the result.

How do you convert hexadecimal numbers?

The conversion of hexadecimal to decimal is done by using the base number 16. The hexadecimal digit is expanded to multiply each digit with the power of 16. The power starts at 0 from the right moving forward towards the right with the increase in power. For the conversion to complete, the multiplied numbers are added.


3 Answers

>>> print int('0x10', 16)
16
like image 142
Ignacio Vazquez-Abrams Avatar answered Sep 30 '22 19:09

Ignacio Vazquez-Abrams


Sample Code :

print "%x"%int("2a",16)
like image 26
Emil Avatar answered Sep 30 '22 19:09

Emil


Are you asking how to convert the string format hexadecimal value '16' into an integer (that is, end up with an integer with decimal value 22)? It's not clear from your question. If so, you probably want int('16', 16)

like image 41
The Archetypal Paul Avatar answered Sep 30 '22 17:09

The Archetypal Paul