Skip to content
Related Articles

Related Articles

Python Program to Convert Decimal to Hexadecimal

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 10 Aug, 2022
Improve Article
Save Article

In this article, we will learn how to convert a decimal value(base 10) to a hexadecimal value (base 16) in Python. 

Method 1: Using hex() function

hex() function is one of the built-in functions in Python3, which is used to convert an integer number into its corresponding hexadecimal form.

Syntax : hex(x) 

Parameters : 

  • x – an integer number (int object)

Returns :  Returns hexadecimal string.

Errors and Exceptions :

TypeError :  Returns TypeError when anything other than

             integer type constants are passed as parameters.   

Code :

Python3




# Python3 program to illustrate
# hex() function
 
print("The hexadecimal form of 69 is "
      + hex(69))

Output:

The hexadecimal form of 69 is 0x45

Method 2: Iterative Approach

The conventional method for converting decimal to hexadecimal is to divide it by 16 until it equals zero. The hexadecimal version of the given decimal number is the sequence of remainders from last to first in hexadecimal form. To convert remainders to hexadecimal form, use the following conversion table:

RemainderHex Equivalent
00
11
22
33
44
55
66
77
88
99
10A
11B
12C
13D
14E
15F

Code :

Python3




# Conversion table of remainders to
# hexadecimal equivalent
conversion_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'}
 
 
# function which converts decimal value
# to hexadecimal value
def decimalToHexadecimal(decimal):
    hexadecimal = ''
    while(decimal > 0):
        remainder = decimal % 16
        hexadecimal = conversion_table[remainder] + hexadecimal
        decimal = decimal // 16
 
    return hexadecimal
 
 
decimal_number = 69
print("The hexadecimal form of", decimal_number,
      "is", decimalToHexadecimal(decimal_number))

Output:

The hexadecimal form of 69 is 45

Method 3: Recursive Approach

The idea is similar to that used in the iterative approach.

Code :

Python3




# Conversion table of remainders to
# hexadecimal equivalent
conversion_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'}
 
 
# function which converts decimal value
# to hexadecimal value
def decimalToHexadecimal(decimal):
    if(decimal <= 0):
        return ''
    remainder = decimal % 16
    return decimalToHexadecimal(decimal//16) + conversion_table[remainder]
 
 
decimal_number = 69
print("The hexadecimal form of", decimal_number,
      "is", decimalToHexadecimal(decimal_number))

Output:

The hexadecimal form of 69 is 45

Method: Using format specifier 

Python3




n =69
print('{0:x}'.format(n))

Output

45

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!