# Convert Hex String to Int in Python

Rayven Esplanada Dec 21, 2022 Dec 05, 2020

This tutorial will demonstrate how to convert the hex string to `int` in Python. It will cover different hex formats like signed, little, and big-endian, `0x` annotated hexadecimal, and the default hex string.

## Use `int()` to Convert Hex to Int in Python

The most common and effective way to convert hex into an integer in Python is to use the type-casting function `int()`.

This function accepts two arguments: one mandatory argument, which is the value to be converted, and a second optional argument, which is the base of the number format with the default as `10`.

Other number formats are `2` for binary, `8` for octal, and `16` for hexadecimal. If you put `0` as the argument for the base value, it will derive the number format from the value’s prefix. If there isn’t any prefix, it will automatically recognize it as a decimal, `0b` for binary, `0o` for octal, and `0x` for hexadecimal.

## Convert Non-Prefixed Hex String to Int in Python

If the hexadecimal string is not prefixed, then specify the base value of the `int()` function to be 16.

For example:

``````hex_val = 'beef101'

print(int(hex_val, 16))
``````

Output:

``````200208641
``````

The result is the decimal or integer conversion of the hex value `beef101`.

## Convert Prefixed Hex String to Int in Python

If the hex string has a prefix `0x`, then change the base value argument to `0` to detect the prefix automatically.

You can still pass `16` as the base, but if you’re dealing with multiple values with different number formats, passing `0` is the best approach.

``````hex_val = '0xdeadcab'

print(int(hex_val, 0))
print(int(hex_val, 16))
``````

Output:

``````233495723
233495723
``````

## Convert Little and Big Endian Hex String to Int in Python

Little endian and big-endia byte orders are two types of ordering systems for hexadecimal. The default order is little-endian, which puts the most significant number in the right-most part of the sequence, while the big-endian does the opposite.

With that in mind, all we have to consider is to convert the big-endian hexadecimal value into a little-endian. Afterward, the usual conversion can now be performed on it.

To convert a big-endian hexadecimal string to a little-endian one, use `bytearray.fromhex()` and use the function `reverse()` on the result. Afterward, convert the hexadecimal value back to string and convert it to an integer.

``````big_endian = 'efbe'

def to_little(val):
little_hex = bytearray.fromhex(val)
little_hex.reverse()
print("Byte array format:", little_hex)

str_little = ''.join(format(x, '02x') for x in little_hex)

return str_little

little_endian = to_little(big_endian)

print("Little endian hex:", little_endian)
print("Hex to int:", int(little_endian, 16))
``````

To summarize this code block:

• Call `bytearray.fromhex()` to convert the big-endian hex string into a byte array hexadecimal value.
• Reverse the byte array to convert the big-endian into a little-endian format.
• Convert the byte array value back into a string hex format in little-endian.
• Convert the string into an integer using `int()`.

Output:

``````Byte array format: bytearray(b'\xbe\xef')
Little endian hex: beef
Hex to int: 48879
``````

## Convert Hex to Signed Integer in Python

Converting any number format into a signed integer would need an operation called the Two’s Complement, which is a bitwise mathematical operation to compute for signed numbers.

So before we can convert hexadecimal into a signed integer, we would need to define a function that will carry out the Two’s Complement operation.

``````def twosComplement_hex(hexval):
bits = 16 # Number of bits in a hexadecimal number format
val = int(hexval, bits)
if val & (1 << (bits-1)):
val -= 1 << bits
return val
``````

The left-most bit in a binary value is called the signed bit, determining if the integer is positive or negative. This function will reserve that bit as the signed bit and shift the other bits to compensate by using the bitwise left shift operator `<<`.

Now, moving on to the actual conversion of the hex value to signed int.

``````hex_val1 = 'ff'
hex_val2 = 'ffff'
hex_val3 = 'aaff'

def twosComplement_hex(hexval):
bits = 16
val = int(hexval, bits)
if val & (1 << (bits-1)):
val -= 1 << bits
return val

print(twosComplement_hex(hex_val1))
print(twosComplement_hex(hex_val2))
print(twosComplement_hex(hex_val3))
``````

Output:

``````255
-1
-21761
``````

Now, we’ve achieved converting a hex string into a signed integer.

In summary, we have covered converting different types of hexadecimal formats into signed and unsigned integers. Use `int()` with the value and base arguments to convert a hex into an unsigned integer.

If the hexadecimal is in a big-endian format, convert it into a little-endian format first using `bytearray.fromhex()` and `reverse()`.

Lastly, if you need to convert a hex string to a signed integer, then perform the Two’s Complement operation on the hexadecimal value on it to get a signed integer value.

Skilled in Python, Java, Spring Boot, AngularJS, and Agile Methodologies. Strong engineering professional with a passion for development and always seeking opportunities for personal and career growth. A Technical Writer writing about comprehensive how-to articles, environment set-ups, and technical walkthroughs. Specializes in writing Python, Java, Spring, and SQL articles.