Python Hexadecimal Integer: Conversion and Formatting
Practical Python guide to converting integers to and from hexadecimal strings, formatting hex output, and using bitwise operations.

When you need to convert between Python integers and hexadecimal strings, the built-in int() and hex() functions cover most cases. But the details of formatting, parsing, and bitwise operations often cause confusion. This article walks through the practical patterns and the edge cases that matter in real code.
Using int() to Parse Hexadecimal Strings
The most direct way to convert a hexadecimal string to an integer is to pass base=16 to int(). The function accepts an optional prefix 0x or 0X when the base is 16.
value = int("0xFF", 16) # 255
value_no_prefix = int("FF", 16) # 255The prefix is optional. If you include it, Python ignores it. If you omit it, the string must contain only valid hexadecimal digits and an optional sign. For example, int("0x1A", 16) works, but int("0x1A", 10) raises a ValueError because 0x is not a valid decimal prefix.
When the input comes from user data or an external system, int() accepts surrounding whitespace, so you do not need to strip it manually. It still raises ValueError for an empty string, so validate the input before conversion if it might be empty.
Using hex() to Convert Integers to Hexadecimal
The hex() built-in returns a string that starts with 0x. For positive integers, the string contains lowercase digits. For negative integers, it returns a string like -0x....
hex(255) # '0xff'
hex(-255) # '-0xff'If you need uppercase digits, use format() or an f-string instead of hex(). The hex() function does not accept formatting options.
Formatting Hexadecimal with format() and f-strings
For more control over the output, use the format() function or f-strings. The format specifier x produces lowercase hex, X produces uppercase, and # adds the base prefix (0x with lowercase, 0X with uppercase).
| Specifier | Example | Output |
|---|---|---|
x | f"{255:x}" | ff |
X | f"{255:X}" | FF |
#x | f"{255:#x}" | 0xff |
#X | f"{255:#X}" | 0XFF |
08x | f"{255:08x}" | 000000ff |
You can combine width and zero-padding. For example, f"{value:08x}" pads the output to eight characters with leading zeros. This is useful when you need a fixed-length representation, such as for color codes or memory addresses.
Hexadecimal Literals in Source Code
Python source code can contain hexadecimal integer literals using the 0x prefix. You can also use underscores to separate groups of digits for readability.
mask = 0xFF00
large_mask = 0xFFFF_0000These literals are integers at runtime. They behave exactly like decimal literals; the base only affects how the value is written in source code. This is useful when you are working with bit masks, network protocols, or memory layouts.
Bitwise Operations on Hexadecimal Integers
Because hexadecimal literals are just integers, you can use bitwise operators directly. This is common when manipulating flags or extracting fields from binary data.
status = 0xD2 # same as 0b1101_0010
high_nibble = (status >> 4) & 0xF
low_nibble = status & 0xFThe &, |, ^, <<, and >> operators work on the integer value. The hexadecimal representation is only a convenience for reading the code; the operations are performed on the underlying binary representation.
Handling Negative Numbers and Two's Complement
hex() returns a string with a leading minus sign for negative integers. This is not the same as a two's complement representation. If you need a fixed-width two's complement string, you must mask the value to the desired bit width.
value = -1
width = 8
two_complement = value & (2**width - 1)
hex_string = f"{two_complement:0{(width + 3) // 4}X}" # 'FF'This pads the output to the number of hex digits needed for the bit width. The mask operation converts the negative integer to its unsigned equivalent within the given bit width. This is common when working with hardware registers or binary protocols that expect unsigned values.
Performance and Maintainability Considerations
For typical conversions, int() and hex() are efficient and clear. Avoid reimplementing conversion logic with loops or string manipulation; the built-ins are optimized and less error-prone.
When you need to format many values, f-strings are more readable than repeated format() calls. For bulk parsing, consider using bytes.fromhex() if you are working with raw bytes, but int(..., 16) is the right choice for a single integer.
Maintainability matters when the code is read by others. Use hexadecimal literals for constants that represent bit patterns, and document the meaning of each mask. A well-named constant like READ_FLAG = 0x01 is clearer than a raw number.
Edge Cases: Large Numbers and Arbitrary Precision
Python integers have arbitrary precision, so you can convert very large hexadecimal strings without overflow. For example:
big = int("FFFFFFFFFFFFFFFF", 16) # 18446744073709551615This is not limited to 32 or 64 bits. The conversion cost grows with input length, but for typical inputs the built-in functions are sufficient. If you are parsing megabytes of hex data, consider streaming or chunking.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.