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 format_map()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python String lstrip()
Python Dictionary values()
Python bool()
Python all()
Python Program to Display Powers of 2 Using Anonymous Function
Python String isprintable()
Python Program to Create a Countdown Timer
Deep Learning in Python - LazyProgrammer
Python hash()
Python Decorators
Python Program to Find Numbers Divisible by Another Number
Python Objects and Classes
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python String capitalize()
Python Package
Python RegEx
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python len()
Python Program to Print the Fibonacci sequence
Python Program to Get the Full Path of the Current Working Directory
Python String splitlines()
Python String isdigit()
Python List
Python Program to Multiply Two Matrices
Python id()
Python List index()
Python Program to Add Two Matrices
Python Dictionary fromkeys()
Python Program to Make a Flattened List from Nested List
Python Set discard()