💬 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
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', base=16) 15 >>> int('0xf', 16) 15
Hexadecimal Number to Integer Without Quotes
Note that you can also write the hexadecimal number without the string quotes like so:
>>> 0xf 15
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') 15
Hex Arithmetic Operators
You can simply add or subtract two hexadecimal numbers in Python by using the normal
>>> 0xf + 0x1 16 >>> 0xf - 0xa 5 >>> 0x1 + 0x1 2
The result is always shown in decimal values, i.e., with
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
Here are a couple of examples:
>>> hex(0x1 + 0x1) '0x2' >>> hex(0xf + 0xf) '0x1e' >>> hex(0xf * 16) '0xf0'
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'—both representing the decimal 15 so the result is decimal 30:
>>> int('0xf', 16) + int('0xf', 16) 30
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
>>> hex(int('0xf', 16) + int('0xf', 16)) '0x1e'
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
* operators, and converting back to hexadecimal strings using the
hex() function on the result.
>>> h1 = '0xf' >>> h2 = '0x1' >>> h1_int = int(h1, 16) >>> h2_int = int(h2, 16) >>> hex(h1_int - h2_int) '0xe' >>> hex(h1_int * h2_int) '0xf'
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:] 'fffffff'
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:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.