The python hex function is used to convert an integer to is an equivalent hexadecimal string.
The syntax of the python hex function is as follows:
hex(x)
hex() Parameter
The python hex function takes one argument as the input from the user.
x- is an integer. The argument is either an int object or it has to define an _index_() method that gives you an integer
Return Value from hex()
The python hexadecimal function returns a hexadecimal string with the 0x format.
Here’s is an example for the same:
Source Code:
print("10 in hexadecimal is: ", hex(10))
print("-5 in hexadecimal is", hex(-5))
val = hex(100) # returns string type
print(type(val))
Output
10 in hexadecimal is: 0xa
-5 in hexadecimal is: '-0x5'
<class 'str'>
To convert a floating-point number to a hexadecimal number, users can use the float.hex().
Source Code:
print("The equivalent value of the hexadecimal 3.9 is: ", float.hex(10.1)) # valid
Output:
0x1.4333333333333p+3
Errors:
In case of any errors, the TypeError exception is returned.
What does hex do in Python?
The hex() in python is used to return a number in the hexadecimal format.
Here are some examples of the python hex().
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'>
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
How do you write hex in python?
To derive a hexadecimal number, the hex() is used. The given number is converted to a hexadecimal value using this function.
Program to convert the decimal value to a hexadecimal form
Source Code:
numb1 = int(input("enter base 10 no\n"))
print("a. Decimal to Hexadecimal ")
print("b. Decimal to Octal")
print("c. Decimal to Binary")
print("Enter your choice :- ")
choice = input()
if choice is 'a':
print("the number in hexadecimal is" + str(number) +
" is " + hex(number).lstrip("0x").rstrip("L"))
if choice is 'b':
print("the number in octal" + str(number) +
" is " + oct(number).lstrip("0o").rstrip("L"))
if choice is 'c':
print("the number i binary" + str(number) +
" is "+bin(number).lstrip("0b").rstrip("L"))
Output :
input variant a)
enter base 10 no
123
Decimal to Hexadecimal
Decimal to Octal
Decimal to Binary
Enter your choice:-
a
The number in hexadecimal is 123 is 7b
How do you find the hex value in Python?
The hex() function is a built-in Python function for converting an integer value to its hexadecimal equivalent. Look at some examples below to understand the working of the hex():
Example 1:
print("The hexadecimal form of 23 is "
+ hex(23))
print(" 'a' in hexadecimal is" + hex(ord('a')))
print("The hexadecimal form of 3.9 is "
+ float.hex(3.9))
Output :
The hexadecimal form of 23 is 0x17
'a' in hexadecimal is 0x61
The hexadecimal form of 3.9 is 0x1.f333333333333p+1
Example 2: The TypeError function is raised when any other datatype rather than an integer is passed as the argument.
Source Code:
print("11.1 hexadecimal format is "
+ hex(11.1))
Output :
Traceback (most recent call last):
File "/home/7e1ac7e34362fd690cdb72cf294502e1.py", line 2, in
print("11.1 in hexadecimal is "+hex(11.1))
The 'float' object cannot be read as an integer, which causes a TypeError.
How do you decipher hex to text in Python?
To convert hexadecimal in python to text, the user will have to implement the bytes.fromhex() AND bytes.decode(). To construct a bytes object, use bytes.fromhex(string) with a hexadecimal string without “0x” at the beginning as a string. To convert this byte object to an ASCII version of the original hexadecimal string, call bytes.decode(encoding) using “ASCII” as encoding.
Source Code
# Remove the leading 0x from the string by slicing it.
hex_string = "0x616263"[2:]
bytes_object = bytes.fromhex(hex_string)
Convert to bytes object
ascii_string = bytes_object.decode("ASCII")
Convert to ASCII representation
print(ascii_string)
OUTPUT
abc
Leave a Reply