HOWTO · Python

How to Convert Hex String to Int in Python

It demonstrates how to convert hexadecimals to integers in Python

On this page

Use int(value, 16) to convert a hexadecimal string to an integer in Python. Use base 0 when you want Python to detect a 0x prefix, and use int.from_bytes() when the input represents bytes with a specified byte order or signed width.

Use int() to Convert Hex to Int in Python

The int() function accepts a string and an optional base. Its default base is 10, so pass 16 for hexadecimal text. In int(value, base), the second argument describes the input string’s base; it is not a bit width. Replace value with the hexadecimal text you need to parse.

Convert Non-Prefixed Hex String to Int in Python

For hexadecimal digits without a 0x or 0X prefix, pass base 16 explicitly. This is useful for values read from text files, APIs, or user input.

hex_string = "1a"
integer_value = int(hex_string, 16)
print(integer_value)
26

The result is 26: base 16 tells int() to interpret each digit in hexadecimal. If the string includes a prefix, either use base 16 or let int() detect the prefix as shown next.

Convert Prefixed Hex String to Int in Python

For prefixed hexadecimal text, base 0 tells int() to infer the base from a recognized prefix such as 0x or 0X. Base 16 also accepts these prefixes; base 0 is useful when input may use other Python integer prefixes too.

hex_string = "0x1a"
integer_value = int(hex_string, 0)
print(integer_value)
26

Because the string starts with 0x, Python parses its remaining digits as hexadecimal and produces 26. Base 0 requires a recognized prefix rather than treating unprefixed text as hexadecimal automatically. Use base 16 when the input omits the prefix.

Convert Little and Big Endian Hex String to Int in Python

Little-endian and big-endian byte orders describe how bytes in a sequence map to an integer; hexadecimal text alone has no default byte order. For the bytes ef be, big-endian places ef first, while little-endian treats ef as the least significant byte. Pass the desired order to int.from_bytes():

data = bytes.fromhex("ef be")
big_value = int.from_bytes(data, byteorder="big")
little_value = int.from_bytes(data, byteorder="little")
print(big_value, little_value)
61374 48879

bytes.fromhex() creates the two-byte sequence, and byteorder controls how those bytes contribute to the result: big-endian produces 61374, while little-endian produces 48879. This keeps byte order explicit without manually reversing a hexadecimal string.

Convert Hex to Signed Integer in Python

An ordinary call such as int("ffff", 16) returns a nonnegative integer. To interpret hexadecimal digits as a signed value, define the byte width and decode those bytes with signed=True. For a 16-bit value, ffff represents -1 in two’s complement:

hex_string = "ffff"
data = bytes.fromhex(hex_string)
signed_value = int.from_bytes(data, byteorder="big", signed=True)
print(signed_value)
-1

bytes.fromhex("ffff") supplies two bytes, so this signed conversion has a 16-bit width and returns -1. The high bit is set, and two’s complement determines the signed value. Choose the byte order and width from the data format you are decoding; int() by itself does not infer either one.