Pass the string and base 16 to Python’s built-in int(): int("FACE", 16) returns 64206. This works whether or not the hexadecimal text starts with 0x.
Convert a hexadecimal string with int()
Use the two-argument form int(text, 16). The second argument tells Python to interpret the string as base 16 rather than the default base 10.
value = int("FACE", 16)
print(value) # 64206
Hexadecimal digits are 0 through 9 and a through f; letter case does not matter. Python’s official built-in functions reference uses int('FACE', 16) as an example and returns 64206.
Handle strings with a 0x prefix
When the base is explicitly 16, the prefix is optional. Both forms below produce the same integer:
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11#1 Best Overall
int("face", 16) # 64206
int("0xface", 16) # 64206
int("0XFACE", 16) # 64206
If the input includes a recognized prefix and you want Python to infer the base, pass 0 instead:
int("0xface", 0) # 64206
Base 0 follows Python’s integer-literal prefix rules, rather than forcing hexadecimal. It also rejects some unprefixed values with leading zeros. If you know the input is hexadecimal, int(text, 16) is the clearer and more direct choice. See the official int() documentation for the accepted bases and prefix behavior.
Rank #2
Accepted characters and common errors
The int() parser allows surrounding whitespace, a leading plus or minus sign with no space between the sign and digits, and single underscores between digits. For example:
int(" -ff ", 16) # -255
int("ab_cd", 16) # 43981
Malformed text raises ValueError. Common causes include an empty string, a character outside the hexadecimal digit set, or passing a base that does not match the text:
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesint("", 16) # ValueError
int("12xz", 16) # ValueError
int("0x20") # ValueError: default base is 10
When input may be invalid, catch the exception and handle it as a parsing failure:
text = "12xz"
try:
value = int(text, 16)
except ValueError:
print("Enter a valid hexadecimal value")
Python’s lexical analysis reference describes integer-literal syntax, including digits, prefixes, and underscores.
Why not use eval()?
Do not use eval() to convert numeric text. It evaluates Python expressions, so untrusted input could do more than represent a number. The Python Programming FAQ also notes that eval() is slower for this purpose and recommends numeric conversion functions instead.
Very long hexadecimal strings in CPython
As documented for CPython 3.14.8, hexadecimal string-to-integer conversions are exempt from the integer string-conversion length limit. The same documentation describes a default limit of 4,300 digits for conversions that are subject to it; that figure does not restrict base-16 conversion. This is a CPython-specific implementation detail, not a general limit guaranteed for every Python implementation. Details are in the Python 3.14.8 built-in types reference.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




