{"id":19465,"date":"2021-08-30T16:06:27","date_gmt":"2021-08-30T10:36:27","guid":{"rendered":"https:\/\/python-programs.com\/?p=19465"},"modified":"2021-11-22T18:36:30","modified_gmt":"2021-11-22T13:06:30","slug":"python-program-to-convert-octal-to-hexadecimal","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-octal-to-hexadecimal\/","title":{"rendered":"Python Program to Convert Octal to Hexadecimal"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Convert Decimal to Hexadecimal<\/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 octal\u00a0number 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 Decimal to Hexadecimal Given an octal number and the task is to get the respective hexadecimal number. Examples: Example1: Input: Given Octal number = 24 Output: The Hexadecimal value of the given octal number { 24 } is: 14 Example2: Input: Given Octal number …<\/p>\n
\nGiven an octal number and the task is to get the respective hexadecimal number.<\/p>\nGiven Octal number = 24<\/pre>\n
The Hexadecimal value of the given octal number { 24 } is: \r\n14<\/pre>\n
Given Octal number = 72<\/pre>\n
The Hexadecimal value of the given octal number { 72 } is: \r\n3A<\/pre>\n
Program to Convert Octal to Hexadecimal in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the octal number as static input and store it in a variable.\r\ngvn_octl_num = 24\r\n# Take a variable say 'k' and initialize its value with 0.\r\nk = 0\r\n# Take a list of length 50 and initialize all the values to zero using the multiplication\r\n# operator.\r\n# Store it in another variable.\r\nHexa_deciml = ['0']*50\r\n# Take another variable say 'deciml_num' and initialize its value with 0.\r\ndeciml_num = 0\r\n# Take another variable say 'tempry' and initialize its value with 0.\r\ntempry = 0\r\nprint(\r\n \"The Hexadecimal value of the given octal number {\", gvn_octl_num, \"} is: \")\r\n# Loop till the given octal number is not equal to zero using the while loop.\r\nwhile gvn_octl_num != 0:\r\n # Inside the loop, calculate the value of the given octal number modulus 10 and store\r\n # it in another variable 'a'.\r\n a = (gvn_octl_num % 10)\r\n # Multiply 'a' with the 8 raised to the power tempry using the pow() function\u00a0and store\r\n# it in another variable 'b'.\r\n b = pow(8, tempry)*a\r\n # Add 'b' with the deciml_num and store it in the same variable deciml_num.\r\n deciml_num = deciml_num+b\r\n # Increment the value of tempry by 1 and store it in the same variable tempry.\r\n tempry += 1\r\n # Divide the given octal number by 10 and store it in the same variable gvn_octl_num.\r\n gvn_octl_num = gvn_octl_num \/\/ 10\r\n# Loop till the decimal number is not equal to zero using the while loop.\r\nwhile deciml_num != 0:\r\n # Calculate the value of deciml_num modulus 16 to get the remainder and store it in a\r\n # variable.\r\n remindr = deciml_num % 16\r\n # Check if the above remainder value is less than 10 using the if conditional statement.\r\n if remindr < 10:\r\n # If the statement is true, then get the character with the ASCII value remainder+48\r\n # using the if conditional statement and store it in a variable\r\n chrvalue = chr(remindr+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 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 chrvalue = chr(remindr+55)\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 # Divide the decimal number by 16 and store it in the same variable deciml_num.\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 of the hexadecimal list to get the\r\n # hexadecimal value of the given octal number.\r\n print(Hexa_deciml[itr], end='')\r\n<\/pre>\n
The Hexadecimal value of the given octal number { 24 } is: \r\n14<\/pre>\n
Method #2: Using While loop (User Input)<\/h3>\n
\n
# Give the octal number as user input using the int(input()) function input and\r\n# store it in a variable.\r\ngvn_octl_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# Take a list of length 50 and initialize all the values to zero using the multiplication\r\n# operator.\r\n# Store it in another variable.\r\nHexa_deciml = ['0']*50\r\n# Take another variable say 'deciml_num' and initialize its value with 0.\r\ndeciml_num = 0\r\n# Take another variable say 'tempry' and initialize its value with 0.\r\ntempry = 0\r\nprint(\r\n \"The Hexadecimal value of the given octal number {\", gvn_octl_num, \"} is: \")\r\n# Loop till the given octal number is not equal to zero using the while loop.\r\nwhile gvn_octl_num != 0:\r\n # Inside the loop, calculate the value of the given octal number modulus 10 and store\r\n # it in another variable 'a'.\r\n a = (gvn_octl_num % 10)\r\n # Multiply 'a' with the 8 raised to the power tempry using the pow() function\u00a0and store\r\n# it in another variable 'b'.\r\n b = pow(8, tempry)*a\r\n # Add 'b' with the deciml_num and store it in the same variable deciml_num.\r\n deciml_num = deciml_num+b\r\n # Increment the value of tempry by 1 and store it in the same variable tempry.\r\n tempry += 1\r\n # Divide the given octal number by 10 and store it in the same variable gvn_octl_num.\r\n gvn_octl_num = gvn_octl_num \/\/ 10\r\n# Loop till the decimal number is not equal to zero using the while loop.\r\nwhile deciml_num != 0:\r\n # Calculate the value of deciml_num modulus 16 to get the remainder and store it in a\r\n # variable.\r\n remindr = deciml_num % 16\r\n # Check if the above remainder value is less than 10 using the if conditional statement.\r\n if remindr < 10:\r\n # If the statement is true, then get the character with the ASCII value remainder+48\r\n # using the if conditional statement and store it in a variable\r\n chrvalue = chr(remindr+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 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 chrvalue = chr(remindr+55)\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 # Divide the decimal number by 16 and store it in the same variable deciml_num.\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 of the hexadecimal list to get the\r\n # hexadecimal value of the given octal number.\r\n print(Hexa_deciml[itr], end='')\r\n<\/pre>\n
Enter some random number = 72\r\nThe Hexadecimal value of the given octal number { 72 } is: \r\n3A<\/pre>\n
\n