In this post, we will learn how to convert decimal to hexadecimal in Python with detailed explanation and example but before we write the program let’s discuss the basics first.

As we know, decimal numbers are a normal number system that consists of 10 digits from 0 to 9 and we will use it in our day to day life for any mathematical calculations whereas hexadecimal numbers are the number system that consists of 16 digits from 0 to F (basically, numbers after 9, like 10, are represented as A, 11 as B, and so on up to 15 as F).

We will use hexadecimal numbers in various computing and programming such as representing binary data and memory addresses.

For a better understanding, the table below shows decimal numbers and their respective hexadecimal equivalents.

Now let’s understand how we can convert a decimal number into a hexadecimal number with the help of an example.

Convert Decimal to Hexadecimal Number

Suppose we have a decimal number 911, and we want to convert it into a hexadecimal number. We can do so by dividing the number 911 by 16 (911 % 16) and storing the remainder. In the next step, take the quotient of the previous division, which is 56, and again divide it by 16 (56 % 16) and store the remainder. Continue this process until you reach 0. At the end, reverse the stored remainders to get the hexadecimal number.

The below figure helps you to understand the process of conversion from a decimal number to a hexadecimal number in more better way.

NOTE: We will replace the remainder with their respective hexadecimal representation if it is greater than 9, like in the above example where we replace 15 with F.

Algorithm

  1. Take a decimal number as an input from the user (decimal_num).
  2. Create a dictionary that contains a decimal number as a key and a value as their respective hexadecimal number in a string.
  3. Create an empty list to store hexadecimal numbers, which is our remainder
    (hex_list = []).
  4. Take a loop while decimal_num !=0 and do
        rem = decimal_num % 16
    hex_list.append(hex_table[rem])
        decimal_num = decimal_num // 16
  5. Reverse the list (hex_list[::-1]) to get hexadecimal number in correct order.
  6. join the list using join() method so that we get hexadecimal number in string from.
  7. At the end Print the hexadecimal number.

NOTE: We created a dictionary in the above algorithm to replace decimal remainders with their respective hexadecimal equivalents.

Now we know how to convert a decimal number to their respective hexadecimal representation so it is time to write the program but before we write a program few programming concepts you have to know:

Source Code

# Taking input from the user
decimal_num = int(input("Enter a Decimal number: ")) 

# Dictionary that contain decimals and their respective hexadecimal numbers  
hex_table = {0: '0', 1: '1', 2: '2', 3: '3', 4: '4',
         5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 
         10: 'A', 11: 'B', 12: 'C',13: 'D', 14: 'E', 15: 'F'} 

# Create a empty list to store hexadecimal numbers 
hex_list = []


# calculation
while decimal_num !=0:
    rem = decimal_num % 16
    hex_list.append(hex_table[rem])
    decimal_num = decimal_num // 16

# Reverse a list using slicing
hex_list = hex_list[::-1] 

# join the list using join() method 
hex_num = "".join(hex_list) 

# print hexadecimal number
print(f"Hexadecimal of given number is : {hex_num}") 

Output-1

Enter a Decimal number: 911
Hexadecimal of given number is : 38F

Output-2

Enter a Decimal number: 1222
Hexadecimal of given number is : 4C6

We can also write above program within one line using built-in function of python.

Using Built-in Function

There is a built-in function called the hex() function; it takes a decimal number as input and returns its hexadecimal representation.

num = int(input("Enter a decimal number: "))
hex_num = hex(num)
print(f"Hexadecimal of {num} is : {hex_num}")

Output:

Enter a decimal number: 911
Hexadecimal of 911 is : 0x38f

The first two characters, ‘0x,’ represent that it is a hexadecimal number, with the remaining ‘38f‘ being the actual hexadecimal value.

This is all about how to convert a decimal number into a hexadecimal number. I hope you understand it well. If you found this post helpful in increasing your knowledge, please consider sharing it with your friends and classmates.

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment