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 staticmethod()
Python time Module
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Program to Check the File Size
Python format()
Python ord()
Python Errors and Built-in Exceptions
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
How to Get Started With Python?
Python Set issubset()
Python for Loop
Python String format()
Python Statement, Indentation and Comments
Python Modules
Python Operator Overloading
Python Type Conversion and Type Casting
Python String find()
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Find All File with .txt Extension Present Inside a Directory
Python String islower()
Python Program to Parse a String to a Float or Int
Python divmod()
Python globals()
Python Program to Find HCF or GCD
Python Program to Add Two Numbers
Python List insert()
Python Machine Learning - Sebastian Raschka
Python classmethod()
Python String endswith()
Python Set symmetric_difference()
Python Set update()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...