Convert hex string to integer in Python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, the standard way to convert a hexadecimal string to an integer is int(hex_string, 16). That handles uppercase or lowercase digits, optional 0x prefixes, and negative signs, so it is the normal answer unless you have a special parsing requirement.
The important part is the second argument. Without telling int() that the string is base 16, Python will treat ordinary digit strings as decimal by default.
The Standard Conversion
This prints 6719.
That is the normal and most readable approach.
Handling the 0x Prefix
Python accepts the usual hexadecimal prefix when you still provide base 16.
Both work correctly.
If you want Python to infer the base from prefixes such as 0x, 0o, or 0b, use base 0 instead.
That is useful when the input format may vary and the prefix is part of the contract.
Case Does Not Matter
Hex digits A-F and a-f are treated the same way.
All of these return 255.
Normalize External Input
If the string comes from a file, command line, or network message, strip surrounding whitespace before converting.
That small normalization step prevents avoidable parsing failures caused by spaces or trailing newlines.
Negative Hex Strings Work Too
A leading minus sign is valid.
This is useful when textual protocols or tools represent signed values in hexadecimal form.
Invalid Input Raises ValueError
If the string is not valid hexadecimal, int() raises ValueError.
That is the correct behavior. If the input is external or untrusted, catch the exception or validate the text before converting.
The Reverse Operation
If you also need to convert an integer back to hexadecimal text, use hex().
This returns '0x1a3f'.
That makes simple round trips easy:
Hex Text Versus Raw Bytes
A common source of confusion is mixing up:
- text like
"ff" - bytes like
b"\xff"
Those are different inputs. The first is hexadecimal text and should be parsed with int(..., 16). The second is raw binary data and may need int.from_bytes(...) or another byte-level API depending on the task.
A Small Parsing Helper
If hexadecimal parsing appears in several places, a helper function can make the accepted rules explicit.
This is also a good place to normalize prefixes, reject empty strings, or wrap ValueError with a clearer application-specific error message.
Common Pitfalls
The biggest mistake is forgetting the base argument and writing int("ff"), which fails because Python assumes base 10. Another is confusing a hexadecimal text string with a bytes object. Developers also often forget to normalize whitespace when input comes from outside the program. Finally, if input quality is uncertain, do not assume every string is valid hex; catch ValueError and handle bad input explicitly.
Summary
- Use
int(hex_string, 16)for ordinary hexadecimal string conversion. - Python accepts uppercase, lowercase, optional
0xprefixes, and negative signs. - Use
strip()when external input may contain whitespace. - Use base
0if you want Python to infer the base from prefixes. - Catch
ValueErrorwhen input may not be valid hexadecimal.

