Python Program to Convert Hexadecimal to Decimal
You can convert a hexadecimal string to decimal in Python using
int(hex_string, 16), where hex_string is your hexadecimal number as a string.Examples
Input1A
Output26
InputFF
Output255
Input0
Output0
How to Think About It
To convert a hexadecimal number to decimal, understand that hexadecimal uses base 16 with digits 0-9 and letters A-F. Each digit represents a power of 16. The program reads the hex string and calculates the decimal value by multiplying each digit by 16 raised to its position power, then sums all these values.
Algorithm
1
Get the hexadecimal number as a string input.2
Use a built-in function or manually convert each hex digit to its decimal equivalent.3
Multiply each digit by 16 raised to the power of its position from right to left.4
Sum all these values to get the decimal number.5
Return or print the decimal number.Code
python
hex_num = input("Enter a hexadecimal number: ") dec_num = int(hex_num, 16) print(f"Decimal value: {dec_num}")
Output
Enter a hexadecimal number: 1A
Decimal value: 26
Dry Run
Let's trace the input '1A' through the code
1
Input hexadecimal
User inputs '1A'
2
Convert using int()
int('1A', 16) converts '1A' to decimal 26
3
Print result
Prints 'Decimal value: 26'
| Hex Digit | Decimal Value | Position (power of 16) | Calculation |
|---|---|---|---|
| A | 10 | 16^0 = 1 | 10 * 1 = 10 |
| 1 | 1 | 16^1 = 16 | 1 * 16 = 16 |
Why This Works
Step 1: Using int() with base 16
The int() function can convert strings in any base to decimal by specifying the base as 16 for hexadecimal.
Step 2: Hexadecimal digits
Hex digits include 0-9 and A-F, where A=10, B=11, ..., F=15, which int() understands automatically.
Step 3: Output decimal value
The converted decimal number is printed as the final result.
Alternative Approaches
Manual conversion using loop
python
hex_num = input("Enter a hexadecimal number: ") dec_num = 0 hex_digits = '0123456789ABCDEF' hex_num = hex_num.upper() for i, digit in enumerate(reversed(hex_num)): dec_num += hex_digits.index(digit) * (16 ** i) print(f"Decimal value: {dec_num}")
This method shows how to convert manually without built-in functions but is longer and less efficient.
Using int() with error handling
python
hex_num = input("Enter a hexadecimal number: ") try: dec_num = int(hex_num, 16) print(f"Decimal value: {dec_num}") except ValueError: print("Invalid hexadecimal number")
This approach adds error handling to manage invalid inputs gracefully.
Complexity: O(n) time, O(1) space
Time Complexity
The conversion scans each character once, so time grows linearly with the length of the hexadecimal string.
Space Complexity
Only a few variables are used, so space is constant regardless of input size.
Which Approach is Fastest?
Using Python's built-in int() is fastest and simplest compared to manual conversion.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Built-in int() function | O(n) | O(1) | Quick and reliable conversion |
| Manual loop conversion | O(n) | O(1) | Learning how conversion works |
| int() with error handling | O(n) | O(1) | Robust user input handling |
Use
int(your_hex_string, 16) for a quick and reliable conversion.Forgetting to pass 16 as the base to
int() causes wrong results or errors.