| by Arround The Web | No comments

Python Hex() Function

The “hexadecimal” or “hex” representation is a numerical system that utilizes a base of “16”. This numerical system uses the digits “0” to “9” and the letters/characters “A” to “F”, representing the values from “0” to “15”. To convert the specified integers into hexadecimal representation, the inbuilt “hex()” function is utilized in Python.

This article will provide a comprehensive tutorial on the “hex()” function using numerous examples via the provided content:

What is the Python “hex()” Function?

In Python, the “hex()” function is used to retrieve the hexadecimal form of the specified input integers value.

Syntax

Check out the syntax of the above-described function:

hex(number)

 

In the above syntax, the “number” parameter represents any positive or negative integer value.

Return Value

The hex() function in Python retrieves the string that always starts with the prefix “0x”

Example 1: Converting the Integers to Hexadecimal Numbers Using the “hex()” Function

In the below example code, the “hex()” function is used to convert the decimal, binary, and octal values to a hexadecimal number.

decimal_value = 455
print(hex(decimal_value))

binary_value = 0b11
print(hex(binary_value))

octal_value = 0o66
print(hex(octal_value))

hexadecimal_value = 0XAF
print(hex(hexadecimal_value))

 

The hexadecimal value for each input value has been retrieved successfully:

Example 2: Convert ASCII Characters to Hexadecimal Numbers Using the “hex()” Function

The “hex()” function can also be used to get the hexadecimal number of the ASCII character and float value. To get the hexadecimal of ASCII characters, the “ord()” function is used along with the “hex()” function. To get the hexadecimal for float, the “float.hex()” function is used instead of the simple “hex()” function:

ascii_value = '$'
print(hex(ord(ascii_value)))

float_value = 45.89
print(float.hex(float_value))

 

The above code retrieved the following hexadecimal number:

Example 3: Using the “hex()” Function with Object

In the below-provided code, the class is defined with the “__index__()” method that retrieves the “self.z” value. The created object is assigned the value of “36”, which is passed to the “hex()” function to retrieve the hexadecimal representation:

class sample:
    z = 0
    def __index__(self):
        print('Function Called!')
        return self.z

obj = sample()
obj.z = 36
print(hex(obj))

 

The above code execution retrieves the following output:

Example 4: Exception Handling While Using “hex()” Function

An exception, “TypeError”, can arise when the float value is directly passed to the “hex()” function. For example, let’s have a look at the below example:

To handle this exception, we need to use the “try-except” block. The try block raises the error, and the exception block handles the exception by using the “float.hex()” function. The “float.hex()” function retrieves the hexadecimal value of the float value:

try:
    print(hex(2.5))
except:
    print(float.hex(2.5))

 

The above code executes the below output:

Conclusion

The “hex()” function is used in Python for converting particular integer numbers to the hexadecimal number representation. We can use this function to convert integers, ASCII characters, and others to hexadecimal numbers. The float value can be converted into hexadecimal using the “float.hex()” function. This blog demonstrated a comprehensive tutorial on the “hex()” function using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply