{"id":19332,"date":"2021-08-30T16:06:24","date_gmt":"2021-08-30T10:36:24","guid":{"rendered":"https:\/\/python-programs.com\/?p=19332"},"modified":"2021-11-22T18:37:07","modified_gmt":"2021-11-22T13:07:07","slug":"python-program-to-convert-hexadecimal-to-octal","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-hexadecimal-to-octal\/","title":{"rendered":"Python Program to Convert Hexadecimal To Octal"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Convert Hexadecimal to Decimal<\/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 hexadecimal number into octal :<\/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 Decimal Given a hexadecimal number and the task is to get the respective octal number. Examples: Example1: Input: Given Hexadecimal String = “5B” Output: The Octal value of the given Hexadecimal number { 5B } is: 133 Example2: Input: Given Hexadecimal String …<\/p>\n
\nGiven a hexadecimal number and the task is to get the respective octal number.<\/p>\nGiven Hexadecimal String = \"5B\"<\/pre>\n
The Octal value of the given Hexadecimal number { 5B } is: \r\n133<\/pre>\n
Given Hexadecimal String = \"3DA\"<\/pre>\n
The Octal value of the given Hexadecimal number { 3DA } is: \r\n1732<\/pre>\n
Program to Convert Hexadecimal To Octal in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the hexadecimal string as static input and store it in a variable.\r\nhexa_num = \"5B\"\r\n# Take a variable say 'rslt_val 'and initialize its value with 0.\r\nrslt_val = 0\r\n# Take another variable say deciml_num and initialize its value with 0.\r\ndeciml_num = 0\r\n# Calculate the length of the given hexadecimal number and store it in a variable k.\r\nk = len(hexa_num)\r\n# Decrement the value of k by 1 and store it in the same\u00a0variable k.\r\nk -= 1\r\nprint(\"The Octal value of the given Hexadecimal number {\", hexa_num, \"} is: \")\r\n# Loop from 0 to the length of the given hexadecimal number using the for loop.\r\nfor itr in range(0, len(hexa_num)):\r\n # Inside the loop, check if the iterator value of the hexadecimal string is greater than or\r\n # equal 0 and less than or equal to 9 using the if conditional statement.\r\n if hexa_num[itr] >= '0' and hexa_num[itr] <= '9':\r\n # If it is true, then convert the iterator value of the hexadecimal string into an integer\r\n # using the int() function.\r\n # Assign its value to the above-initialized rslt_val.\r\n rslt_val = (int)(hexa_num[itr])\r\n# Check if the iterator value of the hexadecimal string is equal to 'A' or 'a' using the\r\n# if conditional statement.\r\n# If it is true, then assign value 10 to the above-initialized rslt_val.\r\n if hexa_num[itr] == 'A' or hexa_num[itr] == 'a':\r\n rslt_val = 10\r\n # Check if the iterator value of the hexadecimal string is equal to 'B' or 'b' using the\r\n# if conditional statement.\r\n# If it is true, then assign value 11 to the above-initialized rslt_val.\r\n if hexa_num[itr] == 'B' or hexa_num[itr] == 'b':\r\n rslt_val = 11\r\n # Check if the iterator value of the hexadecimal string is equal to 'C' or 'c' using the\r\n# if conditional statement.\r\n# If it is true, then assign value 12 to the above-initialized rslt_val.\r\n if hexa_num[itr] == 'C' or hexa_num[itr] == 'c':\r\n rslt_val = 12\r\n # Check if the iterator value of the hexadecimal string is equal to 'D' or 'd' using the\r\n# if conditional statement.\r\n# If it is true, then assign value 13 to the above-initialized rslt_val.\r\n if hexa_num[itr] == 'D' or hexa_num[itr] == 'd':\r\n rslt_val = 13\r\n # Check if the iterator value of the hexadecimal string is equal to 'E' or 'e' using the\r\n# if conditional statement.\r\n# If it is true, then assign value 14 to the above-initialized rslt_val.\r\n if hexa_num[itr] == 'E' or hexa_num[itr] == 'e':\r\n rslt_val = 14\r\n# Check if the iterator value of the hexadecimal string is equal to 'F' or 'f' using the\r\n# if conditional statement.\r\n# If it is true, then assign value 15 to the above-initialized rslt_val.\r\n if hexa_num[itr] == 'F' or hexa_num[itr] == 'f':\r\n rslt_val = 15\r\n # Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it\r\n# into integer using the int() function.\r\n# Add this with the\u00a0deciml_num and store it in the same variable deciml_num.\r\n deciml_num = deciml_num+(int)(rslt_val*math.pow(16, k))\r\n # Decrease the value of k by 1 and store it in the same variable k.\r\n k -= 1\r\n# Take a variable say 'tempry\u00a0'and initialize its value with 1.\r\ntempry = 1\r\n# Take another variable say octl_num and initialize its value with 0.\r\noctl_num = 0\r\n# Loop till the decimal number is not equal to zero using the while loop.\r\nwhile(deciml_num != 0):\r\n # Multiply the above-initialized tempry variable with the decimal number modulus 8 and\r\n # store it in a variable 'a'.\r\n a = (deciml_num % 8)*tempry\r\n # Add 'a' with the above-initialized octl_num and store it in the same variable octl_num.\r\n octl_num = octl_num+a\r\n # Divide the decimal number by 8 and store it in the same variable deciml_num.\r\n deciml_num = deciml_num\/\/8\r\n# Multiply the above variable tempry with 10 and convert it into an integer using the int()function.\r\n# Store it in the same variable\u00a0tempry.\r\n tempry = int(tempry*10)\r\n# Print the octl_num to get the octal value of the given hexadecimal number.\r\nprint(octl_num)\r\n<\/pre>\n
The Octal value of the given Hexadecimal number { 5B } is: \r\n133<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the hexadecimal string as user input using the input() function \r\n# and store it in a variable.\r\nhexa_num = input(\"Enter some Random String Number = \")\r\n# Take a variable say 'rslt_val 'and initialize its value with 0.\r\nrslt_val = 0\r\n# Take another variable say deciml_num and initialize its value with 0.\r\ndeciml_num = 0\r\n# Calculate the length of the given hexadecimal number and store it in a variable k.\r\nk = len(hexa_num)\r\n# Decrement the value of k by 1 and store it in the same\u00a0variable k.\r\nk -= 1\r\nprint(\"The Octal value of the given Hexadecimal number {\", hexa_num, \"} is: \")\r\n# Loop from 0 to the length of the given hexadecimal number using the for loop.\r\nfor itr in range(0, len(hexa_num)):\r\n # Inside the loop, check if the iterator value of the hexadecimal string is greater than or\r\n # equal 0 and less than or equal to 9 using the if conditional statement.\r\n if hexa_num[itr] >= '0' and hexa_num[itr] <= '9':\r\n # If it is true, then convert the iterator value of the hexadecimal string into an integer\r\n # using the int() function.\r\n # Assign its value to the above-initialized rslt_val.\r\n rslt_val = (int)(hexa_num[itr])\r\n# Check if the iterator value of the hexadecimal string is equal to 'A' or 'a' using the\r\n# if conditional statement.\r\n# If it is true, then assign value 10 to the above-initialized rslt_val.\r\n if hexa_num[itr] == 'A' or hexa_num[itr] == 'a':\r\n rslt_val = 10\r\n # Check if the iterator value of the hexadecimal string is equal to 'B' or 'b' using the\r\n# if conditional statement.\r\n# If it is true, then assign value 11 to the above-initialized rslt_val.\r\n if hexa_num[itr] == 'B' or hexa_num[itr] == 'b':\r\n rslt_val = 11\r\n # Check if the iterator value of the hexadecimal string is equal to 'C' or 'c' using the\r\n# if conditional statement.\r\n# If it is true, then assign value 12 to the above-initialized rslt_val.\r\n if hexa_num[itr] == 'C' or hexa_num[itr] == 'c':\r\n rslt_val = 12\r\n # Check if the iterator value of the hexadecimal string is equal to 'D' or 'd' using the\r\n# if conditional statement.\r\n# If it is true, then assign value 13 to the above-initialized rslt_val.\r\n if hexa_num[itr] == 'D' or hexa_num[itr] == 'd':\r\n rslt_val = 13\r\n # Check if the iterator value of the hexadecimal string is equal to 'E' or 'e' using the\r\n# if conditional statement.\r\n# If it is true, then assign value 14 to the above-initialized rslt_val.\r\n if hexa_num[itr] == 'E' or hexa_num[itr] == 'e':\r\n rslt_val = 14\r\n# Check if the iterator value of the hexadecimal string is equal to 'F' or 'f' using the\r\n# if conditional statement.\r\n# If it is true, then assign value 15 to the above-initialized rslt_val.\r\n if hexa_num[itr] == 'F' or hexa_num[itr] == 'f':\r\n rslt_val = 15\r\n # Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it\r\n# into integer using the int() function.\r\n# Add this with the\u00a0deciml_num and store it in the same variable deciml_num.\r\n deciml_num = deciml_num+(int)(rslt_val*math.pow(16, k))\r\n # Decrease the value of k by 1 and store it in the same variable k.\r\n k -= 1\r\n# Take a variable say 'tempry\u00a0'and initialize its value with 1.\r\ntempry = 1\r\n# Take another variable say octl_num and initialize its value with 0.\r\noctl_num = 0\r\n# Loop till the decimal number is not equal to zero using the while loop.\r\nwhile(deciml_num != 0):\r\n # Multiply the above-initialized tempry variable with the decimal number modulus 8 and\r\n # store it in a variable 'a'.\r\n a = (deciml_num % 8)*tempry\r\n # Add 'a' with the above-initialized octl_num and store it in the same variable octl_num.\r\n octl_num = octl_num+a\r\n # Divide the decimal number by 8 and store it in the same variable deciml_num.\r\n deciml_num = deciml_num\/\/8\r\n# Multiply the above variable tempry with 10 and convert it into an integer using the int()function.\r\n# Store it in the same variable\u00a0tempry.\r\n tempry = int(tempry*10)\r\n# Print the octl_num to get the octal value of the given hexadecimal number.\r\nprint(octl_num)\r\n<\/pre>\n
Enter some Random String Number = 3DA\r\nThe Octal value of the given Hexadecimal number { 3DA } is: \r\n1732<\/pre>\n
\n