Decimal to hexadecimal in Python
In this post, you will learn how to convert decimal to hexadecimal using the Python programming language.
A decimal number is a base-10 number system where the range of numbers is 0,1,2,3,4,5,6,7,8,9. The main advantages of decimal number system are that it is easy-to-read, used by humans, and easy to manipulate. But there are some disadvantages, like wastage of space and time.
Hexadecimal describes a base-16 number system. The hexadecimal Number System is generally used in computer programming and Microprocessors. It is also helpful to describe colours on web pages. The main advantage of a Hexadecimal Number is that it is exceptionally compact and, by utilizing a base of 16 means that the number of digits used to address a given number is normally less than in binary or decimal.
Convert decimal to hexadecimal using hex() function
Python provides a built-in hex() function to convert various values to a hexadecimal number. This function returns hexadecimal in the form of a string prefixed with 0x.
print("10 : ",hex(10))
print("26 : ",hex(26))
print("21 : ",hex(21))
print("6 : ",hex(6))
print("8 : ",hex(8))
Output of the above code:
10 : 0xa
26 : 0x1a
21 : 0x15
6 : 0x6
8 : 0x8
Convert decimal to hexadecimal using Loop
For converting a decimal to hexadecimal, the standard mathematical approach is to divide the number by 16 until it reduces to zero. The sequence of remainders from last to first in a hex structure is the hexadecimal number system of the given decimal number.
conversionTable = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A' , 'B', 'C', 'D', 'E', 'F']
decimal = int(input("Enter a number: "))
hexadecimal = ''
while(decimal>0):
remainder = decimal%16
hexadecimal = conversionTable[remainder]+ hexadecimal
decimal = decimal//16
print("Hexadecimal: ",hexadecimal)
Output of the above code:
Enter a number: 26
Hexadecimal: 1A
Enter a number: 10
Hexadecimal: A
Enter a number: 11
Hexadecimal: B
Convert Decimal to Hexadecimal using Recursion
In the given Python program, we have used the recursion function to convert decimal to hexadecimal.
conversionTable = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A' , 'B', 'C', 'D', 'E', 'F']
def decTohex(n):
if(n<=0):
return ''
remainder = n%16
return decTohex(n//16)+conversionTable[remainder]
decimal = int(input("Enter a number: "))
print("Hexadecimal: ",decTohex(decimal))
Output of the above code:
Enter a number: 26
Hexadecimal: 1A
Enter a number: 52
Hexadecimal: 34
Related Articles
Python convert list to numpy array
Python Numpy Array Shape
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Trigonometric functions Python Numpy
Python Pandas Dataframe to CSV
Python Pandas DataFrame
Convert list to dictionary Python
Dictionary inside list Python
Convert dictionary to dataframe Python
Python Pandas CSV to Dataframe
Convert List to Dataframe Python
Python add list to list
Difference between tuple and list in Python
Convert Excel to CSV Python Pandas
Alphabetical order Python
Python | Generate QR Code using pyqrcode module