The hex() function is one of the built-in functions in Python3, which converts an integer number into its corresponding hexadecimal form.
Python hex
Python hex() is a built-in function that converts an integer number ( in base 10) to the corresponding hexadecimal number. Notably, the given input should be in base 10. The hex() function converts the integer to the corresponding hexadecimal number in string form and returns it.
The input integer argument can be in any base, such as binary, octal, etc. Python will take care of converting them to hexadecimal format.
Syntax
hex(number)
Parameters
number: It is an integer that will be converted into a hexadecimal value.
This function converts the number into the hexadecimal form, then returns that hexadecimal number in a string format.
Please note that the return value always starts with ‘0x’ (without quotes), which proves that the number is in hexadecimal format.
# app.py print("Enter the number: ") # taking input from user num = int(input()) # converting the number into hexadecimal form h1 = hex(num) # Printing hexadecimal form print("The ", num, " in hexadecimal is: ", h1) # Converting float number to hexadecimal form print("\nEnter a float number") num2 = float(input()) # converting into hexadecimal form # for float we have to use float.hex() here h2 = float.hex(num2) # printing result print("The ", num2, " in hexadecimal is: ", h2)
In the above example, we used the Python input() function to take the input from the user.
See the output.
Enter the number: 541 The 541 in hexadecimal is: 0x21d Enter a float number 123.54 The 123.54 in hexadecimal is: 0x1.ee28f5c28f5c3p+6
Python hex() without 0x
Let’s see an example of the hex() method without 0x.
See the following program.
# app.py print("Enter the number: ") # taking input from user num = int(input()) # converting the number into hexadecimal form h1 = hex(num) # Printing hexadecimal form # we have used string slicing here print("The ", num, " in hexadecimal is: ", h1[2:]) # Converting float number to hexadecimal form print("\nEnter a float number") num2 = float(input()) # converting into hexadecimal form h2 = float.hex(num2) # printing result print("The ", num2, " in hexadecimal is: ", h2[2:])
See the output.
Enter the number: 541 The 541 in hexadecimal is: 21d Enter a float number 123.65 The 123.65 in hexadecimal is: 1.ee9999999999ap+6
In the above program, we have used string slicing to print the result without ‘0x’.
We have started our index from position 2 to the last of the string, i.e., h1[2:]; this means the string will print characters from position 2 to the last.
Hexadecimal representation of float in Python
Let’s see an example of the hexadecimal representation of float.
See the following program.
# app.py numberEL = 11.21 print(numberEL, 'in hex =', float.hex(numberEL)) numberK = 19.21 print(numberK, 'in hex =', float.hex(numberK))
See the output.
➜ pyt python3 app.py 11.21 in hex = 0x1.66b851eb851ecp+3 19.21 in hex = 0x1.335c28f5c28f6p+4 ➜ pyt
Python hex() with object
See the following code.
# app.py class AI: id = 0 def __index__(self): print('__index__() function called') return self.rank stockfish = AI() stockfish.rank = 2900 print(hex(stockfish))
In the above example, we have used the __index__() method with the hex() function.
See the output.
➜ pyt python3 app.py __index__() function called 0xb54 ➜ pyt
How to convert hex string to int in Python
Without the 0x prefix, you need to specify the base explicitly. Otherwise, it won’t work.
See the following code.
# app.py data = int("0xa", 16) print(data)
With the 0x prefix, Python can distinguish hex and decimal automatically.
You must specify 0 as the base to invoke this prefix-guessing behavior; omitting the second parameter means to assume base-10.)
If you want to convert the string to an int, pass the string to an int along with a base you are converting from. Both strings will suffice for conversion in this way.
# app.py hexStrA = "0xffff" hexStrB = "ffff" print(int(hexStrA, 16)) print(int(hexStrB, 16))
See the output.
➜ pyt python3 app.py 65535 65535 ➜ pyt
We have used the Python int() method in the above examples.
That’s it for the hex() method in Python.