![]() |
VOOZH | about |
hex() function in Python is used to convert an integer to its hexadecimal equivalent. It takes an integer as input and returns a string representing the number in hexadecimal format, starting with "0x" to indicate that it's in base-16. Example:
0xff
Explanation: hex() function takes that number and converts it into its hexadecimal representation, which is 0xff. The result is stored in res.
hex(x)
Parameter: x is an integer number (of type int).
Returns: A string representing the hexadecimal form of the integer. The string is prefixed with 0x, indicating that it is in base-16.
Example 1: In this example, we convert an ASCII character and a floating-point number to their corresponding hexadecimal values.
0x61 0x1.f333333333333p+1
Explanation:
Example 2: In this example, we perform bitwise AND and bitwise OR operations on two hexadecimal numbers a and b. The results are then converted to hexadecimal format.
0x2 0x2a
Explanation:
Example 3: In this example, we convert a large integer to its corresponding hexadecimal value using the hex() function.
0x3ade68b1
Explanation: hex(a) convert 987654321 to its hexadecimal representation.
hex() function raises a TypeError if a non-integer (e.g., a float or string) is passed as an argument.
Output
Hangup (SIGHUP)
Traceback (most recent call last):
File "/home/guest/sandbox/Solution.py", line 1, in <module>
print(hex(11.1))
~~~^^^^^^
TypeError: 'float' object cannot be interpreted as an integer
To convert a float to hexadecimal, use the float.hex() method instead.
0x1.6333333333333p+3