# Python Program to Convert Hexadecimal To Octal

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 = "3DA"

Output:

The Octal value of the given Hexadecimal number { 3DA } is:
1732

## Program to Convert Hexadecimal To Octal in Python

Below are the ways to convert the given hexadecimal number into octal :

### Method #1: Using For Loop (Static Input)

Approach:

• Import math module using the import keyword.
• Give the hexadecimal string as static input and store it in a variable.
• Take a variable say ‘rslt_val ‘and initialize its value with 0.
• Take another variable say deciml_num and initialize its value with 0.
• Calculate the length of the given hexadecimal number and store it in a variable k.
• Decrement the value of k by 1 and store it in the sameÂ variable k.
• Loop from 0 to the length of the given hexadecimal number using the for loop.
• Inside the loop, check if the iterator value of the hexadecimal string is greater than or equal to 0 and less than or equal to 9 using the if conditional statement.
• If it is true, then convert the iterator value of the hexadecimal string into an integer using the int() function.
• Assign its value to the above-initialized rslt_val.
• Check if the iterator value of the hexadecimal string is equal to ‘A’ or ‘a’ using the if conditional statement.
• If it is true, then assign value 10 to the above-initialized rslt_val.
• Check if the iterator value of the hexadecimal string is equal to ‘B’ or ‘b’ using the if conditional statement.
• If it is true, then assign value 11 to the above-initialized rslt_val.
• Check if the iterator value of the hexadecimal string is equal to ‘C’ or ‘c’ using the if conditional statement.
• If it is true, then assign value 12 to the above-initialized rslt_val.
• Check if the iterator value of the hexadecimal string is equal to ‘D’ or ‘d’ using the if conditional statement.
• If it is true, then assign value 13 to the above-initialized rslt_val.
• Check if the iterator value of the hexadecimal string is equal to ‘E’ or ‘e’ using the if conditional statement.
• If it is true, then assign value 14 to the above-initialized rslt_val.
• Check if the iterator value of the hexadecimal string is equal to ‘F’ or ‘f’ using the if conditional statement.
• If it is true, then assign value 15 to the above-initialized rslt_val.
• Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it into integer using the int() function.
• Add this with theÂ deciml_num and store it in the same variable deciml_num.
• Decrease the value of k by 1 and store it in the same variable k.
• Take a variable say ‘tempry ‘and initialize its value with 1.
• Take another variable say octl_num and initialize its value with 0.
• Loop till the decimal number is not equal to zero using the while loop.
• Multiply the above-initialized tempry variable with the decimal number modulus 8 and store it in a variable ‘a’.
• Add ‘a’ with the above-initialized octl_num and store it in the same variable octl_num.
• Divide the decimal number by 8 and store it in the same variable deciml_num.
• Multiply the above variable tempry with 10 and convert it into an integer using the int() function.
• Store it in the same variableÂ tempry.
• Print the octl_num to get the octal value of the given hexadecimal number.
• The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the hexadecimal string as static input and store it in a variable.
hexa_num = "5B"
# Take a variable say 'rslt_val 'and initialize its value with 0.
rslt_val = 0
# Take another variable say deciml_num and initialize its value with 0.
deciml_num = 0
# Calculate the length of the given hexadecimal number and store it in a variable k.
k = len(hexa_num)
# Decrement the value of k by 1 and store it in the sameÂ variable k.
k -= 1
print("The Octal value of the given Hexadecimal number {", hexa_num, "} is: ")
# Loop from 0 to the length of the given hexadecimal number using the for loop.
for itr in range(0, len(hexa_num)):
# Inside the loop, check if the iterator value of the hexadecimal string is greater than or
# equal 0 and less than or equal to 9 using the if conditional statement.
if hexa_num[itr] >= '0' and hexa_num[itr] <= '9':
# If it is true, then convert the iterator value of the hexadecimal string into an integer
# using the int() function.
# Assign its value to the above-initialized rslt_val.
rslt_val = (int)(hexa_num[itr])
# Check if the iterator value of the hexadecimal string is equal to 'A' or 'a' using the
# if conditional statement.
# If it is true, then assign value 10 to the above-initialized rslt_val.
if hexa_num[itr] == 'A' or hexa_num[itr] == 'a':
rslt_val = 10
# Check if the iterator value of the hexadecimal string is equal to 'B' or 'b' using the
# if conditional statement.
# If it is true, then assign value 11 to the above-initialized rslt_val.
if hexa_num[itr] == 'B' or hexa_num[itr] == 'b':
rslt_val = 11
# Check if the iterator value of the hexadecimal string is equal to 'C' or 'c' using the
# if conditional statement.
# If it is true, then assign value 12 to the above-initialized rslt_val.
if hexa_num[itr] == 'C' or hexa_num[itr] == 'c':
rslt_val = 12
# Check if the iterator value of the hexadecimal string is equal to 'D' or 'd' using the
# if conditional statement.
# If it is true, then assign value 13 to the above-initialized rslt_val.
if hexa_num[itr] == 'D' or hexa_num[itr] == 'd':
rslt_val = 13
# Check if the iterator value of the hexadecimal string is equal to 'E' or 'e' using the
# if conditional statement.
# If it is true, then assign value 14 to the above-initialized rslt_val.
if hexa_num[itr] == 'E' or hexa_num[itr] == 'e':
rslt_val = 14
# Check if the iterator value of the hexadecimal string is equal to 'F' or 'f' using the
# if conditional statement.
# If it is true, then assign value 15 to the above-initialized rslt_val.
if hexa_num[itr] == 'F' or hexa_num[itr] == 'f':
rslt_val = 15
# Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it
# into integer using the int() function.
# Add this with theÂ deciml_num and store it in the same variable deciml_num.
deciml_num = deciml_num+(int)(rslt_val*math.pow(16, k))
# Decrease the value of k by 1 and store it in the same variable k.
k -= 1
# Take a variable say 'tempryÂ 'and initialize its value with 1.
tempry = 1
# Take another variable say octl_num and initialize its value with 0.
octl_num = 0
# Loop till the decimal number is not equal to zero using the while loop.
while(deciml_num != 0):
# Multiply the above-initialized tempry variable with the decimal number modulus 8 and
# store it in a variable 'a'.
a = (deciml_num % 8)*tempry
# Add 'a' with the above-initialized octl_num and store it in the same variable octl_num.
octl_num = octl_num+a
# Divide the decimal number by 8 and store it in the same variable deciml_num.
deciml_num = deciml_num//8
# Multiply the above variable tempry with 10 and convert it into an integer using the int()function.
# Store it in the same variableÂ tempry.
tempry = int(tempry*10)
# Print the octl_num to get the octal value of the given hexadecimal number.
print(octl_num)


Output:

The Octal value of the given Hexadecimal number { 5B } is:
133

### Method #2: Using For loop (User Input)

Approach:

• Import math module using the import keyword.
• Give the hexadecimal string as user input using the input() function and store it in a variable.
• Take a variable say ‘rslt_val ‘and initialize its value with 0.
• Take another variable say deciml_num and initialize its value with 0.
• Calculate the length of the given hexadecimal number and store it in a variable k.
• Decrement the value of k by 1 and store it in the sameÂ variable k.
• Loop from 0 to the length of the given hexadecimal number using the for loop.
• Inside the loop, check if the iterator value of the hexadecimal string is greater than or equal to 0 and less than or equal to 9 using the if conditional statement.
• If it is true, then convert the iterator value of the hexadecimal string into an integer using the int() function.
• Assign its value to the above-initialized rslt_val.
• Check if the iterator value of the hexadecimal string is equal to ‘A’ or ‘a’ using the if conditional statement.
• If it is true, then assign value 10 to the above-initialized rslt_val.
• Check if the iterator value of the hexadecimal string is equal to ‘B’ or ‘b’ using the if conditional statement.
• If it is true, then assign value 11 to the above-initialized rslt_val.
• Check if the iterator value of the hexadecimal string is equal to ‘C’ or ‘c’ using the if conditional statement.
• If it is true, then assign value 12 to the above-initialized rslt_val.
• Check if the iterator value of the hexadecimal string is equal to ‘D’ or ‘d’ using the if conditional statement.
• If it is true, then assign value 13 to the above-initialized rslt_val.
• Check if the iterator value of the hexadecimal string is equal to ‘E’ or ‘e’ using the if conditional statement.
• If it is true, then assign value 14 to the above-initialized rslt_val.
• Check if the iterator value of the hexadecimal string is equal to ‘F’ or ‘f’ using the if conditional statement.
• If it is true, then assign value 15 to the above-initialized rslt_val.
• Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it into integer using the int() function.
• Add this with theÂ deciml_num and store it in the same variable deciml_num.
• Decrease the value of k by 1 and store it in the same variable k.
• Take a variable say ‘tempry ‘and initialize its value with 1.
• Take another variable say octl_num and initialize its value with 0.
• Loop till the decimal number is not equal to zero using the while loop.
• Multiply the above-initialized tempry variable with the decimal number modulus 8 and store it in a variable ‘a’.
• Add ‘a’ with the above-initialized octl_num and store it in the same variable octl_num.
• Divide the decimal number by 8 and store it in the same variable deciml_num.
• Multiply the above variable tempry with 10 and convert it into an integer using the int() function.
• Store it in the same variableÂ tempry.
• Print the octl_num to get the octal value of the given hexadecimal number.
• The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the hexadecimal string as user input using the input() function
# and store it in a variable.
hexa_num = input("Enter some Random String Number = ")
# Take a variable say 'rslt_val 'and initialize its value with 0.
rslt_val = 0
# Take another variable say deciml_num and initialize its value with 0.
deciml_num = 0
# Calculate the length of the given hexadecimal number and store it in a variable k.
k = len(hexa_num)
# Decrement the value of k by 1 and store it in the sameÂ variable k.
k -= 1
print("The Octal value of the given Hexadecimal number {", hexa_num, "} is: ")
# Loop from 0 to the length of the given hexadecimal number using the for loop.
for itr in range(0, len(hexa_num)):
# Inside the loop, check if the iterator value of the hexadecimal string is greater than or
# equal 0 and less than or equal to 9 using the if conditional statement.
if hexa_num[itr] >= '0' and hexa_num[itr] <= '9':
# If it is true, then convert the iterator value of the hexadecimal string into an integer
# using the int() function.
# Assign its value to the above-initialized rslt_val.
rslt_val = (int)(hexa_num[itr])
# Check if the iterator value of the hexadecimal string is equal to 'A' or 'a' using the
# if conditional statement.
# If it is true, then assign value 10 to the above-initialized rslt_val.
if hexa_num[itr] == 'A' or hexa_num[itr] == 'a':
rslt_val = 10
# Check if the iterator value of the hexadecimal string is equal to 'B' or 'b' using the
# if conditional statement.
# If it is true, then assign value 11 to the above-initialized rslt_val.
if hexa_num[itr] == 'B' or hexa_num[itr] == 'b':
rslt_val = 11
# Check if the iterator value of the hexadecimal string is equal to 'C' or 'c' using the
# if conditional statement.
# If it is true, then assign value 12 to the above-initialized rslt_val.
if hexa_num[itr] == 'C' or hexa_num[itr] == 'c':
rslt_val = 12
# Check if the iterator value of the hexadecimal string is equal to 'D' or 'd' using the
# if conditional statement.
# If it is true, then assign value 13 to the above-initialized rslt_val.
if hexa_num[itr] == 'D' or hexa_num[itr] == 'd':
rslt_val = 13
# Check if the iterator value of the hexadecimal string is equal to 'E' or 'e' using the
# if conditional statement.
# If it is true, then assign value 14 to the above-initialized rslt_val.
if hexa_num[itr] == 'E' or hexa_num[itr] == 'e':
rslt_val = 14
# Check if the iterator value of the hexadecimal string is equal to 'F' or 'f' using the
# if conditional statement.
# If it is true, then assign value 15 to the above-initialized rslt_val.
if hexa_num[itr] == 'F' or hexa_num[itr] == 'f':
rslt_val = 15
# Multiply rslt_val with 16 raised to the power k using math.pow() function and convert it
# into integer using the int() function.
# Add this with theÂ deciml_num and store it in the same variable deciml_num.
deciml_num = deciml_num+(int)(rslt_val*math.pow(16, k))
# Decrease the value of k by 1 and store it in the same variable k.
k -= 1
# Take a variable say 'tempryÂ 'and initialize its value with 1.
tempry = 1
# Take another variable say octl_num and initialize its value with 0.
octl_num = 0
# Loop till the decimal number is not equal to zero using the while loop.
while(deciml_num != 0):
# Multiply the above-initialized tempry variable with the decimal number modulus 8 and
# store it in a variable 'a'.
a = (deciml_num % 8)*tempry
# Add 'a' with the above-initialized octl_num and store it in the same variable octl_num.
octl_num = octl_num+a
# Divide the decimal number by 8 and store it in the same variable deciml_num.
deciml_num = deciml_num//8
# Multiply the above variable tempry with 10 and convert it into an integer using the int()function.
# Store it in the same variableÂ tempry.
tempry = int(tempry*10)
# Print the octl_num to get the octal value of the given hexadecimal number.
print(octl_num)


Output:

Enter some Random String Number = 3DA
The Octal value of the given Hexadecimal number { 3DA } is:
1732

Explore more instances related to python concepts fromÂ Python Programming ExamplesÂ Guide and get promoted from beginner to professional programmer level in Python Programming Language.