int()to Convert Hex to Int in Python
- Convert Non-Prefixed 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 big-endian,
0x annotated hexadecimal, and the default hex string.
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
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
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.
hex_val = 'beef101' print(int(hex_val, 16))
The result is the decimal or integer conversion of the hex value
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))
Convert Little and Big Endian Hex String to Int in Python
Little endian and big-endian 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:
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
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))
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
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.