Python hex Function
Last modified April 11, 2025
This comprehensive guide explores Python's hex function, which
converts integers to hexadecimal strings. We'll cover basic usage, formatting,
error handling, and practical examples of hexadecimal conversion.
Basic Definitions
The hex function converts an integer to a lowercase hexadecimal
string prefixed with "0x". Hexadecimal is base-16 number system using digits
0-9 and letters a-f.
Key characteristics: works only with integers, returns a string, handles both positive and negative numbers. The output is always prefixed with "0x".
Basic Integer Conversion
Here's simple usage with different integers showing how hex
converts positive, negative, and zero values to hexadecimal.
# Positive integers print(hex(10)) # 0xa print(hex(255)) # 0xff # Negative integers print(hex(-10)) # -0xa print(hex(-255)) # -0xff # Zero print(hex(0)) # 0x0
This example shows hex with different integer values. Positive
numbers get standard conversion, negatives keep their sign, and zero becomes
"0x0".
The prefix "0x" indicates hexadecimal format, similar to how Python literals are written. Letters are always lowercase.
Large Integer Conversion
The hex function handles arbitrarily large integers, making it
useful for cryptographic and low-level programming applications.
# Very large numbers big_num = 2**64 - 1 print(hex(big_num)) # 0xffffffffffffffff # Extremely large numbers huge_num = 10**100 print(hex(huge_num)) # 0x1249ad2594c37ceb0b2784c4ce0bf38ace408e211a7caab24308a82e8f10000000000000000000000000
These examples demonstrate hex with very large integers. Python's
arbitrary-precision integers mean there's no practical limit to size.
The hexadecimal representation grows with the number's magnitude, showing Python's ability to handle big numbers seamlessly.
Custom Object Conversion
You can make custom objects work with hex by implementing the
__index__ special method. This example creates a custom number class.
class CustomNumber:
def __init__(self, value):
self.value = value
def __index__(self):
return self.value
def __repr__(self):
return f"CustomNumber({self.value})"
num = CustomNumber(42)
print(hex(num)) # 0x2a
The CustomNumber class implements __index__ to return its integer
value. When we call hex on an instance, Python uses this method.
This pattern is useful for numeric wrapper classes that need to support hexadecimal conversion while maintaining their type.
Error Handling
The hex function raises TypeError when used with
non-integer types. This example shows proper error handling.
try:
print(hex("hello"))
except TypeError as e:
print(f"Error: {e}") # 'str' object cannot be interpreted as an integer
class NoIndex:
pass
try:
print(hex(NoIndex()))
except TypeError as e:
print(f"Error: {e}") # 'NoIndex' object cannot be interpreted as an integer
These examples demonstrate hex's behavior with unsupported types.
Strings and objects without __index__ raise TypeError.
To make a class work with hex, implement __index__
as shown in the previous example.
Hexadecimal Formatting Alternatives
This example compares hex with other methods for hexadecimal
string formatting.
num = 255
# Using hex()
print(hex(num)) # 0xff
# Using format()
print(format(num, '#x')) # 0xff
print(format(num, 'x')) # ff
print(format(num, '04x')) # 00ff
# Using f-strings
print(f"{num:#x}") # 0xff
print(f"{num:x}") # ff
print(f"{num:04x}") # 00ff
This shows different ways to format numbers as hexadecimal strings. hex
provides the simplest interface but less control than format or f-strings.
The format specifiers allow control over prefix inclusion, padding, and case,
offering more flexibility than hex alone.
Best Practices
- Use for readability: Prefer
hexfor simple conversions - Use format for control: When you need padding or uppercase
- Implement __index__: For custom numeric types needing hex conversion
- Handle errors: Catch TypeError when input type is uncertain
- Document behavior: Clearly document __index__ implementation
Source References
Author
List all Python tutorials.