Table of Contents
The hex() function converts an integer number to the corresponding hexadecimal string.
The syntax of hex() is:
hex(x)
1. hex() Parameters
hex() function takes a single argument.
x – integer number (int object or it has to define __index__() method that returns an integer)
2. Return Value from hex()
hex() function converts an integer to the corresponding hexadecimal number in string form and returns it.
The returned hexadecimal string starts with the prefix 0x indicating it’s in hexadecimal form.
3. Example 1: How hex() works?
number = 435
print(number, 'in hex =', hex(number))
number = 0
print(number, 'in hex =', hex(number))
number = -34
print(number, 'in hex =', hex(number))
returnType = type(hex(number))
print('Return type from hex() is', returnType)
Output
435 in hex = 0x1b3 0 in hex = 0x0 -34 in hex = -0x22 Return type from hex() is <class 'str'>
If you need to find a hexadecimal representation of a float, you need to use float.hex() method.
4. Example 2: Hexadecimal representation of a float
number = 2.5 print(number, 'in hex =', float.hex(number)) number = 0.0 print(number, 'in hex =', float.hex(number)) number = 10.5 print(number, 'in hex =', float.hex(number))
Output
2.5 in hex = 0x1.4000000000000p+1 0.0 in hex = 0x0.0p+0 10.5 in hex = 0x1.5000000000000p+3
Related posts:
Python String isdecimal()
Python Program Read a File Line by Line Into a List
Python Program to Display Fibonacci Sequence Using Recursion
Python String islower()
Python Program to Find Hash of File
Python Set difference_update()
Python Program to Access Index of a List Using for Loop
Python compile()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Find the Square Root
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Intelligent Projects Using Python - Santanu Pattanayak
Python Strings
Python Program to Get Line Count of a File
Python Program to Check If a List is Empty
Python Functions
Python String find()
Python Program to Find the Factors of a Number
Python Program to Check If Two Strings are Anagram
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Matrices and NumPy Arrays
Python List reverse()
Python Program to Find Armstrong Number in an Interval
Python range()
Deep Learning with Python - Francois Chollet
Python Dictionary copy()
Python String partition()
How to Get Started With Python?
Python String maketrans()
Python Program to Iterate Over Dictionaries Using for Loop
Node.js vs Python for Backend Development
Python Program to Differentiate Between type() and isinstance()