Python Program to Convert Decimal to Hexadecimal Last Updated : 16 Dec, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 0x45Method 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 Equivalent0011223344556677889910A11B12C13D14E15F 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 45Method 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 45Method: Using format specifier Python3 n =69 # Here 69 is the number to be converted into hexadecimal value # now we use 'd' for decimal, 'o' for octal, 'x' for hexadecimal as format specifier to format a number # the result is in type of "String" but you can typecaste using int() if necessary print('{0:x}'.format(n)) Output45 Comment More infoAdvertise with us Next Article Python program to convert exponential to float C chirags_30 Follow Improve Article Tags : Python Python Programs Blogathon Blogathon-2021 Numbers +1 More Practice Tags : Numberspython Similar Reads Python program to convert hex string to decimal Converting a hexadecimal string to a decimal number is a common task in programming, especially when working with binary data or low-level computations. Hexadecimal values offer a concise way to represent large numbers.Using the int() FunctionUsing the int() function is the most common and straightf 2 min read Python program to convert hex string to decimal Converting a hexadecimal string to a decimal number is a common task in programming, especially when working with binary data or low-level computations. Hexadecimal values offer a concise way to represent large numbers.Using the int() FunctionUsing the int() function is the most common and straightf 2 min read Python Program to Convert Binary to Hexadecimal Given a binary number, the task is to write a Python program to convert the given binary number into an equivalent hexadecimal number. i.e convert the number with base value 2 to base value 16. In hexadecimal representation we 16 values to represent a number. Numbers 0-9 are expressed by digits 0-9 4 min read Python program to convert int to exponential Given a number of int type, the task is to write a Python program to convert it to exponential. Examples: Input: 19 Output: 1.900000e+01 Input: 2002 Output: 2.002000e+03 Input: 110102 Output: 1.101020e+05Approach: We will first declare and initialise an integer numberThen we will use format method t 1 min read Python program to convert exponential to float Given a number in exponential format, the task is to write a Python program to convert the number from exponential format to float. The exponential number is a way of representing a number. Examples: Input: 1.900000e+01 Output: 19.0 Input: 2.002000e+03 Output: 2002.0 Input: 1.101020e+05 Output: 1101 1 min read Python program to convert float to exponential Given a float number, the task is to write a Python program to convert float to exponential. Examples: Input: 19.0 Output: 1.900000e+01 Input: 200.2 Output: 2.002000e+02 Input: 1101.02 Output: 1.101020e+03Approach: We will first declare and initialise a float number.Then we will use format method to 1 min read Like