Decimal to Hexadecimal Conversion in Python

python
conversion
decimal
hexadecimal
number

This article provides Python code snippets for converting decimal numbers to hexadecimal and vice versa. Let’s dive into the code!

Decimal to Hexadecimal Conversion

Here’s a Python script that converts a decimal number to its hexadecimal equivalent.

# Python script for decimal to Hexadecimal conversion
# (Example : Input = 65024 Output = FE00)

table = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']

In1 = input("Input, Enter a decimal number = ")
In1_decimal = int(In1)

hexdec1 = ''

while (In1_decimal > 0):
    remainder1 = In1_decimal % 16
    hexdec1 = table[remainder1] + hexdec1
    In1_decimal = In1_decimal // 16

print("Output, Hexadecimal number is = ", hexdec1)

Explanation:

  1. table: This list maps remainders (0-15) to their corresponding hexadecimal characters.

  2. Input: The script prompts the user to enter a decimal number.

  3. Conversion Loop: The while loop repeatedly divides the decimal number by 16. The remainder of each division gives us a hexadecimal digit. These digits are looked up in the table and prepended to the hexdec1 string to build the hexadecimal representation.

  4. Output: The resulting hexadecimal number is printed.

Example Input and Output:


Input, Enter a decimal number = 65024
Output, Hexadecimal number is = FE00

Hexadecimal to Decimal Conversion

Now, let’s look at the Python script for converting a hexadecimal number to its decimal equivalent.

# Python script for Hexadecimal to Decimal conversion
# (Example : Input = AB5F , Output = 43871)

table1 = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15}

hexadecimal2 = input("Input, Enter the hexadecimal number = ").strip().upper()
decimal = 0

# computing max power value
power = len(hexadecimal2) - 1

for digit in hexadecimal2:
    decimal += table1[digit] * 16 ** power
    power -= 1

print("Output, decimal number is =", decimal)

Explanation:

  1. table1: This dictionary maps hexadecimal characters to their corresponding decimal values.

  2. Input: The script prompts the user to enter a hexadecimal number. .strip() removes leading/trailing whitespaces, and .upper() converts the input to uppercase for case-insensitive handling.

  3. Conversion Loop: The for loop iterates through each digit of the hexadecimal number. For each digit:

    • It retrieves the decimal value of the digit from table1.
    • It multiplies this value by 16 raised to the power of its position (starting from the rightmost digit as power 0).
    • It adds the result to the decimal variable.
    • The power variable is decremented for the next digit.
  4. Output: The resulting decimal number is printed.

Example Input and Output:

Input, Enter the hexadecimal number = AB5F
Output, decimal number is = 43871

These scripts provide a clear and concise way to perform decimal to hexadecimal and hexadecimal to decimal conversions in Python. You can easily adapt and integrate them into your own projects.