Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Hex String to Hex Integer in Python
1
Hex String to Hex Integer in Python

5/5 – (1 vote)

[Image: image-268-1024x765.png]

[Image: 1f4ac.png] Question: Given a hexadecimal string such as
Code:
'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
Code:
'0x'
prefix indicates that the digits of the numbers do have a hexadecimal base
Code:
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! [Image: 1f447.png]

Convert Hex to Decimal using int()

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

Code:
>>> 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:

Code:
>>> 0xf 15

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

Using the eval() Function

[Image: 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')
function like so:

Code:
>>> eval('0xf') 15

However, I wouldn’t recommend it over the
Code:
int()
function as the
Code:
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
Code:
+
and
Code:
-
operators:

Code:
>>> 0xf + 0x1 16 >>> 0xf - 0xa 5 >>> 0x1 + 0x1 2

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

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

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

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:
16
which essentially shifts the whole number one digit and inserts a digit at the right—much like multiplying with base
Code:
10
in a decimal system.

Adding Two Hex Strings

[Image: image-270-1024x683.png]

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

Code:
>>> int('0xf', 16) + int('0xf', 16) 30

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

Code:
>>> 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
Code:
16
representations using
Code:
int(hex_str, 16)
, doing the computation in the decimal system using the normal
Code:
-
and
Code:
*
operators, and converting back to hexadecimal strings using the
Code:
hex()
function on the result.

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'
Printing Hex String without Prefix ‘0x’

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

A minimal example:

Code:
>>> hex_string = '0xfffffff' >>> hex_string[2:] 'fffffff'
Where to Go From Here?

[Image: 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]

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

Users browsing this thread:
1 Guest(s)


Forum Jump: