Python hex() Function

Python Built In Hex() Function

This is a tutorial of the Python built-in hex() Function. Learn to convert a given integer number into its corresponding hexadecimal value with examples.

Python hex() Function

This function returns a hexadecimal string that corresponds to a given integer number. Therefore, you can use this function to do the integer to hexadecimal conversion.

Note. If you want to convert a floating-point number into its corresponding hexadecimal value, you may use the float.hex() method.

Syntax

Given below is the syntax for the Python built-in hex() function.

hex() Parameters

This function only takes a single argument and that is the integer number which you wanted to be converted into its corresponding hexadecimal form.

Note. You can also pass another class object that is not even an integer but that class should further have the __index__() method returning an integer number.

hex() Return Value

The integer number passed in the argument is converted into its hexadecimal form and is returned as a string by this function. The string so returned starts with the prefix “0x”. This indicates that the string is actually a hexadecimal number.

Examples

Given below are two examples demonstrating the conversion of integer and floating-point numbers into their corresponding hexadecimal values. The first one shows the conversion of integers by using the Python built-in hex() function while the second one illustrates the conversion of floating-point numbers using the float.hex() method.

Example 1. Converting integer numbers into hexadecimal values using hex() function

num1 = 500
hex1 = hex(num1)
print("Hexadecimal form of", num1, "is", hex1)

#Hexadecimal value is returned as a string
print("Type of num1 is", type(hex1))

num2 = -37
hex2 = hex(num2)
print("Hexadecimal form of", num2, "is", hex2)

num3 = 0
hex3 = hex(num3)
print("Hexadecimal form of", num3, "is", hex3)

Output.

Example 2. Floating-point numbers into the hexadecimal form using float.hex() method

You simply can’t use the built-in function hex() to get the hexadecimal values for the floating-point numbers. Therefore, you must use the float.hex() method instead.

num1 = 0.00
hex1 = num1.hex()
print("Hexadecimal form of", num1, "is", hex1)

#Hexadecimal value is returned as a string
print("Type of num1 is", type(hex1))

num2 = 15.45
hex2 = num2.hex()
print("Hexadecimal form of", num2, "is", hex2)

num3 = 3.0
hex3 = num3.hex()
print("Hexadecimal form of", num3, "is", hex3)

num4 = -5.39349
hex4 = num4.hex()
print("Hexadecimal form of", num4, "is", hex4)

Output.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *