Hex String to Hex Integer in Python

4.5/5 - (2 votes)

πŸ’¬ Question: Given a hexadecimal string such as '0xf' in Python. How to convert it to a hexadecimal number in Python so that you can perform arithmetic operations such as addition and subtraction?

The hexadecimal string representation with the '0x' prefix indicates that the digits of the numbers do have a hexadecimal base 16.

In this article, I’ll show you how to do some basic conversion and arithmetic computations using the hexadecimal format. So, let’s get started! πŸ‘‡

Convert Hex to Decimal using int()

You can convert any hexadecimal string to a decimal number using the int() function with the base=16 argument. For example, '0xf' can be converted to a decimal number using int('0xf', base=16) or simply int('0xf', 16).

>>> int('0xf', base=16)
>>> int('0xf', 16)

Hexadecimal Number to Integer Without Quotes

Note that you can also write the hexadecimal number without the string quotes like so:

>>> 0xf

The 0x prefix already indicates that it is a hexadecimal number.

Using the eval() Function

That’s why an alternative way to convert a hexadecimal string to a numerical value (integer, base 10) is to use the eval('0xf') function like so:

>>> eval('0xf')

However, I wouldn’t recommend it over the int() function as the eval() function is known to be a bit tricky and poses some security risks.

Hex Arithmetic Operators

You can simply add or subtract two hexadecimal numbers in Python by using the normal + and - operators:

>>> 0xf + 0x1
>>> 0xf - 0xa
>>> 0x1 + 0x1

The result is always shown in decimal values, i.e., with base=10.

You can display the result with base=16 by converting it back to a hexadecimal format using the hex() built-in function. For example, the expression hex(0x1 + 0x1) yields the hexadecimal string representation '0x2'.

Python hex() Function -- Not a Magic Trick

Here are a couple of examples:

>>> hex(0x1 + 0x1)
>>> hex(0xf + 0xf)
>>> hex(0xf * 16)

In the last line, you multiply with the base 16 which essentially shifts the whole number one digit and inserts a 0 digit at the right—much like multiplying with base 10 in a decimal system.

Adding Two Hex Strings

In the following example, you add together two hex strings '0xf' and '0xf'—both representing the decimal 15 so the result is decimal 30:

>>> int('0xf', 16) + int('0xf', 16)

If you need the result as a hex string, you can pass the whole computation into the hex() built-in function to obtain a hexadecimal representation of the decimal 30:

>>> hex(int('0xf', 16) + int('0xf', 16))

Subtracting and Multiplying Two Hex Strings

You can also subtract or multiply two hex strings by converting them to integers from their base 16 representations using int(hex_str, 16), doing the computation in the decimal system using the normal - and * operators, and converting back to hexadecimal strings using the hex() function on the result.

See here:

>>> h1 = '0xf'
>>> h2 = '0x1'
>>> h1_int = int(h1, 16)
>>> h2_int = int(h2, 16)
>>> hex(h1_int - h2_int)
>>> hex(h1_int * h2_int)

Printing Hex String without Prefix ‘0x’

To print the hexadecimal string such as '0xffffff' without the '0x' prefix, you can simply use slicing hex_string[2:] starting from the third character and slice all the way to the right.

A minimal example:

>>> hex_string = '0xfffffff'
>>> hex_string[2:]

Where to Go From Here?

Thanks for reading through the whole article, I’d love to see you around more often in the Finxter community to learn and improve your coding skills. ❀️

If you also want to learn, join our free email academy and download our cheat sheets here: