How to convert hex to int in Python?

In Python, element conversion has been a very useful utility as it offers it in a much simpler way than in other languages. This makes Python a robust language and hence knowledge of interconversions is always a plus for a programmer. This article discusses the hexadecimal string to a decimal number. Let’s discuss certain ways in which this can be performed. 

Method 1: Converting hexadecimal to decimal in Python using int() 

This Python int() function can be used to perform this particular task, adding an argument (16) this function can convert a hexadecimal string number to base sixteen and convert it into an integer at the same time. 

Python3




# initializing string

test_string= 'A'

 

# printing original string

print

The original string : A
The decimal number of hexadecimal string : 10
0
The original string : A
The decimal number of hexadecimal string : 10
1
The original string : A
The decimal number of hexadecimal string : 10
2

The original string : A
The decimal number of hexadecimal string : 10
3
The original string : A
The decimal number of hexadecimal string : 10
4
The original string : A
The decimal number of hexadecimal string : 10
5

 

The original string : A
The decimal number of hexadecimal string : 10
6

The original string : A
The decimal number of hexadecimal string : 10
7

The original string : A
The decimal number of hexadecimal string : 10
8=
Enter Hexadecimal Number: A7
167
0
Enter Hexadecimal Number: A7
167
1
Enter Hexadecimal Number: A7
167
2
Enter Hexadecimal Number: A7
167
3

 

Enter Hexadecimal Number: A7
167
4

print

Enter Hexadecimal Number: A7
167
6

Enter Hexadecimal Number: A7
167
7
Enter Hexadecimal Number: A7
167
8
The original string : A
The decimal number of hexadecimal string : 10
2
The original string : A
The decimal number of hexadecimal string : 10
4
Decimal Value =  2578
1

Output:

The original string : A
The decimal number of hexadecimal string : 10

Method 2: Converting hexadecimal to decimal in Python using ast.literal_eval() 

We can perform this particular function by using a literal evaluation function that predicts the base and converts the number string to its decimal number format. 

Python3




Decimal Value =  2578
2
Decimal Value =  2578
3
Decimal Value =  2578
4
Decimal Value =  2578
5

 

# initializing string

test_string=

Decimal Value =  2578
9

 

# printing original string

print

The original string : A
The decimal number of hexadecimal string : 10
0
The original string : A
The decimal number of hexadecimal string : 10
1
The original string : A
The decimal number of hexadecimal string : 10
2

The original string : A
The decimal number of hexadecimal string : 10
3
The original string : A
The decimal number of hexadecimal string : 10
4
The original string : A
The decimal number of hexadecimal string : 10
5

 

# initializing string8

The original string : A
The decimal number of hexadecimal string : 10
7

The original string : A
The decimal number of hexadecimal string : 10
8= test_string2

 

Enter Hexadecimal Number: A7
167
4

print

Enter Hexadecimal Number: A7
167
6

Enter Hexadecimal Number: A7
167
7
Enter Hexadecimal Number: A7
167
8
The original string : A
The decimal number of hexadecimal string : 10
2
The original string : A
The decimal number of hexadecimal string : 10
4
Decimal Value =  2578
1

Output:

The original string : A
The decimal number of hexadecimal string : 10

Method 3: Converting hexadecimal to decimal in Python using without In-build function

We can perform this particular task also by without an In-build function that predicts the base and converts the number string to its decimal number format using a Python dictionary.

Python3




=1= =3=4=5=6=7=8=5'A'0=7'A'2=5'A'4=7'A'6=5'A'8=7

# printing original string0# printing original string1=5# printing original string3=7# printing original string5=5# printing original string7=7# printing original string9=5print1=7print3=5print5=7

# printing original string0print8=5

The original string : A
The decimal number of hexadecimal string : 10
00=7
The original string : A
The decimal number of hexadecimal string : 10
02=5
The original string : A
The decimal number of hexadecimal string : 10
04=7'A'=5
The original string : A
The decimal number of hexadecimal string : 10
08=7
The original string : A
The decimal number of hexadecimal string : 10
10=5
The original string : A
The decimal number of hexadecimal string : 10
12=7

# printing original string0

The original string : A
The decimal number of hexadecimal string : 10
15=5
The original string : A
The decimal number of hexadecimal string : 10
17=7
The original string : A
The decimal number of hexadecimal string : 10
19=5
The original string : A
The decimal number of hexadecimal string : 10
21=7
The original string : A
The decimal number of hexadecimal string : 10
23=5
The original string : A
The decimal number of hexadecimal string : 10
25=7
The original string : A
The decimal number of hexadecimal string : 10
27=5
The original string : A
The decimal number of hexadecimal string : 10
29
The original string : A
The decimal number of hexadecimal string : 10
30

 

The original string : A
The decimal number of hexadecimal string : 10
31=
The original string : A
The decimal number of hexadecimal string : 10
33
The original string : A
The decimal number of hexadecimal string : 10
0
The original string : A
The decimal number of hexadecimal string : 10
35
The original string : A
The decimal number of hexadecimal string : 10
36

The original string : A
The decimal number of hexadecimal string : 10
8= =6

 

The original string : A
The decimal number of hexadecimal string : 10
40

The original string : A
The decimal number of hexadecimal string : 10
41=
The original string : A
The decimal number of hexadecimal string : 10
43
The original string : A
The decimal number of hexadecimal string : 10
44
The original string : A
The decimal number of hexadecimal string : 10
45 'A'0

 

The original string : A
The decimal number of hexadecimal string : 10
47
The original string : A
The decimal number of hexadecimal string : 10
48
The original string : A
The decimal number of hexadecimal string : 10
49
The original string : A
The decimal number of hexadecimal string : 10
50

The original string : A
The decimal number of hexadecimal string : 10
51
The original string : A
The decimal number of hexadecimal string : 10
8=
The original string : A
The decimal number of hexadecimal string : 10
8
The original string : A
The decimal number of hexadecimal string : 10
2
The original string : A
The decimal number of hexadecimal string : 10
56
The original string : A
The decimal number of hexadecimal string : 10
57
Enter Hexadecimal Number: A7
167
2
The original string : A
The decimal number of hexadecimal string : 10
57
The original string : A
The decimal number of hexadecimal string : 10
57
The original string : A
The decimal number of hexadecimal string : 10
41

The original string : A
The decimal number of hexadecimal string : 10
51
The original string : A
The decimal number of hexadecimal string : 10
41=
The original string : A
The decimal number of hexadecimal string : 10
41
The original string : A
The decimal number of hexadecimal string : 10
45 'A'0

 

print

The original string : A
The decimal number of hexadecimal string : 10
69

Output:

Enter Hexadecimal Number: A7
167

Method 4: Converting hexadecimal to decimal in Python using loop

We can perform this particular function by using a Python loop that predicts the base and converts the number string to its decimal number format. 

Python3




# initializing string

The original string : A
The decimal number of hexadecimal string : 10
71 =
The original string : A
The decimal number of hexadecimal string : 10
73

 

The original string : A
The decimal number of hexadecimal string : 10
74=
The original string : A
The decimal number of hexadecimal string : 10
76=
The original string : A
The decimal number of hexadecimal string : 10
78= =6

 

The original string : A
The decimal number of hexadecimal string : 10
41=
The original string : A
The decimal number of hexadecimal string : 10
43
The original string : A
The decimal number of hexadecimal string : 10
0
The original string : A
The decimal number of hexadecimal string : 10
71
Enter Hexadecimal Number: A7
167
3
The original string : A
The decimal number of hexadecimal string : 10
45 'A'0

The original string : A
The decimal number of hexadecimal string : 10
89

The original string : A
The decimal number of hexadecimal string : 10
90
The original string : A
The decimal number of hexadecimal string : 10
91= =6=5

 

The original string : A
The decimal number of hexadecimal string : 10
51
The original string : A
The decimal number of hexadecimal string : 10
96
The original string : A
The decimal number of hexadecimal string : 10
71
The original string : A
The decimal number of hexadecimal string : 10
98= =4
Enter Hexadecimal Number: A7
167
01
The original string : A
The decimal number of hexadecimal string : 10
71
Enter Hexadecimal Number: A7
167
03=
The original string : A
The decimal number of hexadecimal string : 10
02=5

Enter Hexadecimal Number: A7
167
07
Enter Hexadecimal Number: A7
167
08=
Enter Hexadecimal Number: A7
167
0
The original string : A
The decimal number of hexadecimal string : 10
0
The original string : A
The decimal number of hexadecimal string : 10
71
Enter Hexadecimal Number: A7
167
13

 

The original string : A
The decimal number of hexadecimal string : 10
51
Enter Hexadecimal Number: A7
167
15
The original string : A
The decimal number of hexadecimal string : 10
71
The original string : A
The decimal number of hexadecimal string : 10
98= 'A'
Enter Hexadecimal Number: A7
167
01
The original string : A
The decimal number of hexadecimal string : 10
71
Enter Hexadecimal Number: A7
167
03=
The original string : A
The decimal number of hexadecimal string : 10
27=5

Enter Hexadecimal Number: A7
167
07
Enter Hexadecimal Number: A7
167
08=
Enter Hexadecimal Number: A7
167
29
The original string : A
The decimal number of hexadecimal string : 10
0
The original string : A
The decimal number of hexadecimal string : 10
71
Enter Hexadecimal Number: A7
167
13
The original string : A
The decimal number of hexadecimal string : 10
45
Enter Hexadecimal Number: A7
167
34

 

The original string : A
The decimal number of hexadecimal string : 10
51
Enter Hexadecimal Number: A7
167
15
The original string : A
The decimal number of hexadecimal string : 10
71
The original string : A
The decimal number of hexadecimal string : 10
98=
Enter Hexadecimal Number: A7
167
40
Enter Hexadecimal Number: A7
167
01
The original string : A
The decimal number of hexadecimal string : 10
71
Enter Hexadecimal Number: A7
167
03=
Enter Hexadecimal Number: A7
167
45=5

Enter Hexadecimal Number: A7
167
07
Enter Hexadecimal Number: A7
167
08=
Enter Hexadecimal Number: A7
167
29
The original string : A
The decimal number of hexadecimal string : 10
0
The original string : A
The decimal number of hexadecimal string : 10
71
Enter Hexadecimal Number: A7
167
13
The original string : A
The decimal number of hexadecimal string : 10
45
Enter Hexadecimal Number: A7
167
55

The original string : A
The decimal number of hexadecimal string : 10
51
Enter Hexadecimal Number: A7
167
57=5

Enter Hexadecimal Number: A7
167
07
The original string : A
The decimal number of hexadecimal string : 10
74= 'A'0

Enter Hexadecimal Number: A7
167
07
Enter Hexadecimal Number: A7
167
64

The original string : A
The decimal number of hexadecimal string : 10
51
The original string : A
The decimal number of hexadecimal string : 10
76=
The original string : A
The decimal number of hexadecimal string : 10
76
The original string : A
The decimal number of hexadecimal string : 10
2
Enter Hexadecimal Number: A7
167
70
The original string : A
The decimal number of hexadecimal string : 10
57
The original string : A
The decimal number of hexadecimal string : 10
0
Enter Hexadecimal Number: A7
167
2
The original string : A
The decimal number of hexadecimal string : 10
57
The original string : A
The decimal number of hexadecimal string : 10
57
Enter Hexadecimal Number: A7
167
76

How to convert hexadecimal to int?

To convert a hexadecimal string to a number.
Use the ToInt32(String, Int32) method to convert the number expressed in base-16 to an integer. The first argument of the ToInt32(String, Int32) method is the string to convert. ... .
Note that the hexadecimal string has the following restrictions: It cannot include the &h prefix..

How to convert hex list to int list in Python?

The easiest way to convert a list of hex strings to a list of integers in Python is the list comprehension statement [int(x, 16) for x in my_list] that applies the built-in function int() to convert each hex string to an integer using the hexadecimal base 16 , and repeats this for each hex string x in the original list ...

How do you convert hex to text in Python?

To convert hexadecimal in python to text, the user will have to implement the bytes. fromhex() AND bytes. decode().

What is %02x in Python?

The 02x tells Python we want the number represented as a 2 digit hexadecimal number, where any missing digits are padded with zeroes.