In this post we are going to understand Python Program to convert decimal to hexadecimal with example using built in function hex () and Python convert decimal to hex () without 0x.
1. Python hex() function to convert decimal
In this example we have used Python built-in function hex() to convert an integer number to a lower-case hexadecimal string prefixed with “0x”.
Python Program
dec_num = int(input("Please Enter a number: "))
print("Decimal to hex: ",hex(dec_num))
Output
Please Enter a number: 12
Decimal to hex: 0xc
2. Python convert decimal to hex() without 0x
In this example we are using the inbuilt hex() function to get the hexadecimal number and the slicing [2:] to remove the prefix “ox” and convert decimal to hexadecimal without “0x”
Python Program to convert decimal to hex() without 0x
dec_num = int(input("Please Enter a number: "))
hex_num = hex(dec_num)
print("The decimal to hexadecimal value: ", hex_num[2:])
Output
Please Enter a number: 12
The decimal to hexadecimal value: c
3. Function to convert decimal to hexadecimal Using while loop
In this example we have used a function decimal_to_hexa() to convert decimal to hexadecimal by using while loop with list append () method to get hexadecimal need to divide the decimal number to 16 until it decreases to zero and appending the reminder to list. To get list element reversed order by using [: :-1] and iterating list using for loop to get hexadecimal value.
Python Program to convert decimal to hexadecimal using while loop
def decimal_to_hexa(dec_num):
lst = list()
while dec_num:
reminder = dec_num % 16
dec_num = dec_num // 16
if reminder < 10:
lst.append(reminder)
else:
lst.append(chr(reminder+55))
for num in lst[::-1]:
print(num, end="")
dec_num = int(input("Please Enter a number: "))
decimal_to_hexa(dec_num)
Output
Please Enter a number: 94
5E
4. Python program to convert decimal to hexadecimal Using recursion
In this example, we have defined a function decimalTo_hexadecimal() to call recursively to find the hexadecimal.
- if the decimal number is less than 0 then we will return.
- Next, we are finding reminder by dividing the decimal number by 16 and adding the corresponding list value to reminder and calling the decimalTo_hexadecimal() recursively.
Python Progarm to convert decimal to hexadecimal Using recursion
HexVal_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A' , 'B', 'C', 'D', 'E', 'F']
def decimalTo_hexadecimal(dec_num):
if(dec_num<=0):
return ''
remainder = dec_num%16
return decimalTo_hexadecimal(dec_num//16)+HexVal_list[remainder]
decimal = int(input("Please Enter a number: "))
print("Decimal to Hexadecimal val: ",decimalTo_hexadecimal(decimal))
Output
Please Enter a number: 12
Decimal to Hexadecimal val: C
5. Convert String to Hexdecimal in Python
To convert a string to hexadecimal first we have to convert it to int with base 16.First we have to convert hexadecimal string to number using int() method.Finally using hex() method to convert to hexadecimal.
Python Program to convert String to Hexdecimal in Python
hex_str = "0xBB"
int_num = int(hex_str, 16)
hexadecimal = hex(int_num)
print(hexadecimal)
Output
0xbb
Summary
In this post,We have learned Python Program to convert decimal to hexadecimal with examples