{"id":19219,"date":"2021-08-30T16:06:25","date_gmt":"2021-08-30T10:36:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=19219"},"modified":"2021-11-22T18:36:30","modified_gmt":"2021-11-22T13:06:30","slug":"python-program-to-convert-decimal-to-hexadecimal","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-decimal-to-hexadecimal\/","title":{"rendered":"Python Program to Convert Decimal to Hexadecimal"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Convert Hexadecimal To Octal<\/a> Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to convert the given decimal number into hexadecimal :<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Convert Hexadecimal To Octal Given a decimal number and the task is to get the respective hexadecimal number using the while loop. Examples: Example1: Input: Given decimal number = 30 Output: The Hexadecimal value of the given decimal number { 30 } is: 1E Example2: …<\/p>\n
\nGiven a decimal number and the task is to get the respective hexadecimal number using the while loop.<\/p>\nGiven decimal number = 30<\/pre>\n
The Hexadecimal value of the given decimal number { 30 } is: \r\n1E<\/pre>\n
Given decimal number = 60<\/pre>\n
The Hexadecimal value of the given decimal number { 60 } is: \r\n3C<\/pre>\n
Program to Convert Decimal to Hexadecimal in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the decimal number as static input and store it in a variable.\r\ndeciml_num = 30\r\n# Take a variable say 'k' and initialize its value with 0.\r\nk = 0\r\n#\u00a0Take\u00a0a\u00a0list\u00a0of\u00a0length\u00a050\u00a0and\u00a0initialize\u00a0all\u00a0the\u00a0values\u00a0to\u00a0zero\u00a0using\u00a0the\u00a0multiplication\u00a0operator.\u00a0\r\n#\u00a0Store\u00a0it\u00a0in\u00a0another\u00a0variable.\r\nHexa_deciml = ['0']*50\r\n\r\nprint(\r\n \"The Hexadecimal value of the given decimal number {\", deciml_num, \"} is: \")\r\n# Loop till the given decimal number is not equal to zero using the while loop.\r\nwhile deciml_num != 0:\r\n # Inside the loop, calculate the value of decimal number modulus 16 to get the remainder\r\n # and store it in another variable.\r\n remndr = deciml_num % 16\r\n # Check if the above remainder value is less than 10 using the if conditional statement.\r\n if remndr < 10:\r\n # If the statement is true, then get the character with the ASCII value remainder+48 using\r\n # the if conditional statement and store it in a variable\r\n chrvalue = chr(remndr+48)\r\n # Initialize the hexadecimal list at the index 'k' with the above-calculated character.\r\n Hexa_deciml[k] = chrvalue\r\n # Increment the value of k by 1 and store it in the same variable k.\r\n k += 1\r\n\r\n else:\r\n # Else if the statement is false, get the character with the ASCII value remainder+55.\r\n # Store it in another variable.\r\n Hexa_deciml[k] = chr(remndr+55)\r\n # Increment the value of k by 1 and store it in the same variable k.\r\n k += 1\r\n # Divide the given decimal number by 16 and store it in the same variable.\r\n deciml_num \/\/= 16\r\n# Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.\r\nfor itr in range(k-1, -1, -1):\r\n # Print the element present at the iterator value of the hexadecimal list to get the\r\n # hexadecimal value of the given decimal number.\r\n print(Hexa_deciml[itr], end=\"\")\r\n<\/pre>\n
The Hexadecimal value of the given decimal number { 30 } is: \r\n1E<\/pre>\n
Method #2: Using While loop (User Input)<\/h3>\n
\n
# Give the decimal number as user input using the int(input()) function input and\r\n# store it in a variable.\r\ndeciml_num = int(input(\"Enter some random Number = \"))\r\n# Take a variable say 'k' and initialize its value with 0.\r\nk = 0\r\n#\u00a0Take\u00a0a\u00a0list\u00a0of\u00a0length\u00a050\u00a0and\u00a0initialize\u00a0all\u00a0the\u00a0values\u00a0to\u00a0zero\u00a0using\u00a0the\u00a0multiplication\u00a0operator.\u00a0\r\n#\u00a0Store\u00a0it\u00a0in\u00a0another\u00a0variable.\r\nHexa_deciml = ['0']*50\r\n\r\nprint(\r\n \"The Hexadecimal value of the given decimal number {\", deciml_num, \"} is: \")\r\n# Loop till the given decimal number is not equal to zero using the while loop.\r\nwhile deciml_num != 0:\r\n # Inside the loop, calculate the value of decimal number modulus 16 to get the remainder\r\n # and store it in another variable.\r\n remndr = deciml_num % 16\r\n # Check if the above remainder value is less than 10 using the if conditional statement.\r\n if remndr < 10:\r\n # If the statement is true, then get the character with the ASCII value remainder+48 using\r\n # the if conditional statement and store it in a variable\r\n chrvalue = chr(remndr+48)\r\n # Initialize the hexadecimal list at the index 'k' with the above-calculated character.\r\n Hexa_deciml[k] = chrvalue\r\n # Increment the value of k by 1 and store it in the same variable k.\r\n k += 1\r\n\r\n else:\r\n # Else if the statement is false, get the character with the ASCII value remainder+55.\r\n # Store it in another variable.\r\n Hexa_deciml[k] = chr(remndr+55)\r\n # Increment the value of k by 1 and store it in the same variable k.\r\n k += 1\r\n # Divide the given decimal number by 16 and store it in the same variable.\r\n deciml_num \/\/= 16\r\n# Loop in decreasing order from k-1 to 0 with the stepsize of -1 using the for loop.\r\nfor itr in range(k-1, -1, -1):\r\n # Print the element present at the iterator value of the hexadecimal list to get the\r\n # hexadecimal value of the given decimal number.\r\n print(Hexa_deciml[itr], end=\"\")\r\n<\/pre>\n
Enter some random Number = 60\r\nThe Hexadecimal value of the given decimal number { 60 } is: \r\n3C<\/pre>\n
\n