10-30-2022, 12:57 PM
Hex String to Hex Integer in Python
5/5 – (1 vote)
![[Image: image-268-1024x765.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-268-1024x765.png)
Question: Given a hexadecimal string such as 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 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!![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Convert Hex to Decimal using int()
You can convert any hexadecimal string to a decimal number using the function with the argument. For example, can be converted to a decimal number using or simply .
Hexadecimal Number to Integer Without Quotes
Note that you can also write the hexadecimal number without the string quotes like so:
The prefix already indicates that it is a hexadecimal number.
Using the eval() Function
![[Image: image-269.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-269.png)
That’s why an alternative way to convert a hexadecimal string to a numerical value (integer, base 10) is to use the function like so:
However, I wouldn’t recommend it over the function as the 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:
The result is always shown in decimal values, i.e., with.
You can display the result with by converting it back to a hexadecimal format using the built-in function. For example, the expression yields the hexadecimal string representation .
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FF6e9mlCZWkw%2Fhqdefault.jpg)
Here are a couple of examples:
In the last line, you multiply with the base which essentially shifts the whole number one digit and inserts a digit at the right—much like multiplying with base in a decimal system.
Adding Two Hex Strings
![[Image: image-270-1024x683.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-270-1024x683.png)
In the following example, you add together two hex strings and —both representing the decimal 15 so the result is decimal 30:
If you need the result as a hex string, you can pass the whole computation into the built-in function to obtain a hexadecimal representation of the decimal :
Subtracting and Multiplying Two Hex Strings
You can also subtract or multiply two hex strings by converting them to integers from their base representations using , doing the computation in the decimal system using the normal and operators, and converting back to hexadecimal strings using the function on the result.
See here:
Printing Hex String without Prefix ‘0x’
To print the hexadecimal string such as without the prefix, you can simply use slicing starting from the third character and slice all the way to the right.
A minimal example:
Where to Go From Here?
![[Image: image-271-1024x683.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-271-1024x683.png)
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.![[Image: 2764.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/2764.png)
If you also want to learn, join our free email academy and download our cheat sheets here:
5/5 – (1 vote)
![[Image: image-268-1024x765.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-268-1024x765.png)
Question: Given a hexadecimal string such as Code:
'0xf'The hexadecimal string representation with the
Code:
'0x'Code:
16In this article, I’ll show you how to do some basic conversion and arithmetic computations using the hexadecimal format. So, let’s get started!
![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Convert Hex to Decimal using int()
You can convert any hexadecimal string to a decimal number using the
Code:
int()Code:
base=16Code:
'0xf'Code:
int('0xf', base=16)Code:
int('0xf', 16)Code:
>>> int('0xf', base=16)
15
>>> int('0xf', 16)
15Note that you can also write the hexadecimal number without the string quotes like so:
Code:
>>> 0xf
15The
Code:
0xUsing the eval() Function
![[Image: image-269.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-269.png)
That’s why an alternative way to convert a hexadecimal string to a numerical value (integer, base 10) is to use the
Code:
eval('0xf')Code:
>>> eval('0xf')
15However, I wouldn’t recommend it over the
Code:
int()Code:
eval()Hex Arithmetic Operators
You can simply add or subtract two hexadecimal numbers in Python by using the normal
Code:
+Code:
-Code:
>>> 0xf + 0x1
16
>>> 0xf - 0xa
5
>>> 0x1 + 0x1
2The result is always shown in decimal values, i.e., with
Code:
base=10You can display the result with
Code:
base=16Code:
hex()Code:
hex(0x1 + 0x1)Code:
'0x2'Here are a couple of examples:
Code:
>>> hex(0x1 + 0x1) '0x2'
>>> hex(0xf + 0xf) '0x1e'
>>> hex(0xf * 16) '0xf0'In the last line, you multiply with the base
Code:
16Code:
10Adding Two Hex Strings
![[Image: image-270-1024x683.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-270-1024x683.png)
In the following example, you add together two hex strings
Code:
'0xf'Code:
'0xf'Code:
>>> int('0xf', 16) + int('0xf', 16)
30If you need the result as a hex string, you can pass the whole computation into the
Code:
hex()Code:
30Code:
>>> hex(int('0xf', 16) + int('0xf', 16)) '0x1e'You can also subtract or multiply two hex strings by converting them to integers from their base
Code:
16Code:
int(hex_str, 16)Code:
-Code:
*Code:
hex()See here:
Code:
>>> 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'To print the hexadecimal string such as
Code:
'0xffffff'Code:
'0x'Code:
hex_string[2:]A minimal example:
Code:
>>> hex_string = '0xfffffff'
>>> hex_string[2:] 'fffffff'![[Image: image-271-1024x683.png]](https://blog.finxter.com/wp-content/uploads/2022/10/image-271-1024x683.png)
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.
![[Image: 2764.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/2764.png)
If you also want to learn, join our free email academy and download our cheat sheets here:

