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 Program to Find Armstrong Number in an Interval
Python Program to Print Hello world!
Python Program to Merge Two Dictionaries
Introduction to Scientific Programming with Python - Joakim Sundnes
Python String endswith()
Python Program to Swap Two Variables
Python format()
Python Operator Overloading
Python Program to Sort a Dictionary by Value
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python classmethod()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Program to Convert Celsius To Fahrenheit
Python Program to Remove Duplicate Element From a List
Python Program to Display Powers of 2 Using Anonymous Function
Python input()
Python Program to Catch Multiple Exceptions in One Line
Python String find()
Python @property decorator
Python Numbers, Type Conversion and Mathematics
Python locals()
Python Program to Sort Words in Alphabetic Order
Python Program to Check if a Key is Already Present in a Dictionary
Python Program to Check If Two Strings are Anagram
Python Global Keyword
Python Program to Measure the Elapsed Time in Python
Python open()
Python reversed()
Python Program to Transpose a Matrix
Python vars()
Deep Learning in Python - LazyProgrammer
Python Set remove()