Convert Hex String to Int in Python

Use
int()
to Convert Hex to Int in Python  Convert NonPrefixed Hex String to Int in Python
 Convert Prefixed Hex String to Int in Python
 Convert Little and Big Endian Hex String to Int in Python
 Convert Hex to Signed Integer in Python
This tutorial will demonstrate how to convert the hex string to int
in Python. It will cover different hex formats like signed, little, and bigendian, 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 typecasting 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 NonPrefixed 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 bigendia byte orders are two types of ordering systems for hexadecimal. The default order is littleendian, which puts the most significant number in the rightmost part of the sequence, while the bigendian does the opposite.
With that in mind, all we have to consider is to convert the bigendian hexadecimal value into a littleendian. Afterward, the usual conversion can now be performed on it.
To convert a bigendian hexadecimal string to a littleendian 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 bigendian hex string into a byte array hexadecimal value.  Reverse the byte array to convert the bigendian into a littleendian format.
 Convert the byte array value back into a string hex format in littleendian.
 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 << (bits1)):
val = 1 << bits
return val
The leftmost 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 << (bits1)):
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 bigendian format, convert it into a littleendian 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 howto articles, environment setups, and technical walkthroughs. Specializes in writing Python, Java, Spring, and SQL articles.
LinkedInRelated Article  Python Hex
 Convert Hex to Base64 in Python
 Bitwise XOR of Hex Numbers in Python
 Convert Binary to Hex in Python
 Convert HEX to RGB in Python
 Convert Hex to ASCII in Python
 String to Hex in Python
Related Article  Python Integer
 Convert Int to Binary in Python
 Convert Roman Numerals to Integers in Python
 Convert Integer to Roman Numerals in Python
 Integer Programming in Python
 Convert Boolean Values to Integer in Python
 Convert String to Integer in Python