Decimal to Hexadecimal in Python


In this guide, you will learn 4 different ways to convert decimal to hexadecimal in Python.

Decimal to Hexadecimal Conversion

Decimal numbers are the numbers that we use generally in our day-to-day life. It includes all the numbers from 0 to 9.

Hexadecimal is a base 16 number system. It uses 16 symbols to represent numbers. The symbols are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.

10 is written as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

The decimal number can be represented in hexadecimal form:

(0)10 = (0)16
(2)10 = (2)16
(9)10 = (9)16
(10)10 = (A)16
(15)10 = (F)16
(16)10 = (10)16
(25)10 = (19)16
(26)10 = (1A)16
(35)10 = (23)16
(40)10 = (28)16

Steps to convert decimal to hexadecimal:

  1. Divide the decimal number by 16.
  2. Get the quotient becomes the new number and the remainder becomes the last digit of the hexadecimal number.
  3. If the quotient is greater than 9, replace it with the corresponding symbol.
  4. Repeat the steps until the quotient is 0.
Decimal to Hexadecimal in Python
Decimal to Hexadecimal in Python

The hexadecimal number system is widely used in computers. It is used to represent color values in HTML and CSS, and to represent memory addresses.


Method 1: Using hex() function

The hex() built-in function converts a number from any base to hexadecimal.

It takes a single argument and returns a string with the hexadecimal representation of the number.

The returned string starts with the 0x prefix.

num = int(input("Enter a number: "))

hex_num = hex(num)
print(hex_num)

# to remove 0x prefix
hex_num = hex_num[2:]
print(hex_num)

Output:

Enter a number: 25
0x19
19

Method 2: Using format() function

The format() multi-purpose built-in function in Python. It can perform various tasks like formatting strings, numbers, etc.

It takes two arguments: the number to be converted and the base to which it is to be converted.

For example, if the number is 123 and the base is 16, then the format(123, 'x') is used.

num = int(input("Enter a number: "))
hex_num = format(num, 'x')

print(hex_num)

Output:

Enter a number: 40234
9D2A

Method 3: Using custom function

Let's create your own function using the logic of dividing the number by 16 and getting the remainder.

Steps to convert decimal to hexadecimal:

  1. Create a function that takes an integer decimal number as an argument.
  2. Initialize an empty string to store the hexadecimal number.
  3. Run a while loop until the quotient is greater than 0.
  4. Get the remainder of the number divided by 16.
  5. If the remainder is greater than 9, replace it with the corresponding hexadecimal value.
  6. Append the remainder to the string.
  7. Finally, reverse the string and return it.
# custom function to convert decimal to hexadecimal
def dec2hex(dec):
    hex = ''
    while dec > 0:
        rem = dec % 16
        if rem < 10:
            hex += str(rem)
        else:
            hex += chr(rem + 87)
        dec //= 16
    # return reversed string
    return hex[::-1]

num = int(input("Enter a number: "))
print(dec2hex(num))

Output:

Enter a number: 28560
6F90

Method 4: Using Recursion

Recursion is a technique of solving a problem where the solution depends on solutions to smaller instances of the same problem.

We can use recursion to convert decimal to hexadecimal.

Let's see how:

# recursive function to convert decimal to hexadecimal
def dec2hex(dec):
    if dec == 0:
        return ''
    else:
        rem = dec % 16
        if rem < 10:
            return dec2hex(dec // 16) + str(rem)
        else:
            return dec2hex(dec // 16) + chr(rem + 87)

num = int(input("Enter a number: "))
print(dec2hex(num))

Output:

Enter a number: 7634
1DD2

Conclusion

Now you know 4 different ways by which you can convert decimal to hexadecimal in Python. You can use any of the methods as per your requirement.

Happy Learning!😊