Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

How to Convert a Hexadecimal String to an Integer in Python

Use Python’s built-in int(text, 16) to parse hexadecimal strings, with or without a 0x prefix. Learn accepted syntax, error handling, and when base 0 differs.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
int("", 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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.