Python hex() Function



The Python hex() function is used to convert an integer to a hexadecimal (base-16) format.

Hexadecimal is a numeral system that uses 16 digits, where the first ten are the same as the decimal system (0-9), and the next six are represented by the letters A to F (or a-f), corresponding to the values 10 to 15. When you use the hex() function in Python, it converts a decimal number into its hexadecimal representation, and the result is a string that starts with "0x" followed by the hexadecimal digits.

Syntax

Following is the syntax of python hex() function −

hex(x)

Parameters

This function accepts an integer value as its parameter.

Return Value

This function returns a string representing the hexadecimal value of the given integer.

Example 1

Following is an example of the Python hex() function. Here, we are converting the integer "111" to its hexadecimal representation −

integer_number = 111
hexadecimal_number = hex(integer_number)
print('The hexadecimal value obtained is:', hexadecimal_number)

Output

Following is the output of the above code −

The hexadecimal value obtained is: 0x6f

Example 2

Here, we are using the hex() function to convert a binary and an octal value to their corresponding hexadecimal representation −

binary_number = 0b1010
octal_number = 0o77
binary_to_hexadecimal = hex(binary_number)
octal_to_hexadecimal = hex(octal_number)
print('The hexadecimal value of binary number is:', binary_to_hexadecimal)
print('The hexadecimal value of octal number is:', octal_to_hexadecimal)

Output

The output obtained is as follows −

The hexadecimal value of binary number is: 0xa
The hexadecimal value of octal number is: 0x3f

Example 3

Now, we are removing the "0x" prefix from the output when converting integer value "2108" to its hexadecimal representation using the hex() function −

integer_number = 2108
hexadecimal_noprefix = hex(integer_number)[2:]
print('The hexadecimal value of the integer without prefix is:', hexadecimal_noprefix)

Output

The result produced is as follows −

The hexadecimal value of the integer without prefix is: 83c

Example 4

If we pass a non-integer value to the hex() function, it will raise a TypeError.

Here we will demonstrate the TypeError by passing a floating point value "11.21" to the hex() function −

# Example to demonstrate TypeError
floating_number = 11.21
hexadecimal_number = hex(floating_number)
print('The hexadecimal value of the floating number is:', hexadecimal_number)

Output

We can see in the output that we get a TypeError because we have passed a floating point value to the hex() function −

Traceback (most recent call last):
  File "C:\Users\Lenovo\Desktop\untitled.py", line 3, in <module>
    hexadecimal_number = hex(floating_number)
TypeError: 'float' object cannot be interpreted as an integer

Example 5

Since hexadecimal represents numbers in base-16 notation, we can handle the TypeError by using the float.hex() function instead of the regular hex() function.

# Example without TypeError
floating_number = 11.21
hexadecimal_number = float.hex(floating_number)
print('The hexadecimal value of the floating number is:', hexadecimal_number)

Output

The output of the code is as follows −

The hexadecimal value of the floating number is: 0x1.66b851eb851ecp+3
python_type_casting.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy