Convert hexadecimal to octal number in Python

In this tutorial, we can learn how to convert hexadecimal to octal number in Python.

Number systems can be thought of as notations adapted for representing numbers, by using digits/symbols in a defined and ordered manner.

Human interactions with software usually involve the decimal system owing to its understandability. On the other hand, computers work by making use of binary numbers.
However, the binary system involves only two symbols, thus making it difficult to represent various different states. Hence, we make use of the hexadecimal and octal states for the same.
The hexadecimal number system is a base-16 system where each symbol represents 4 binary digits.
The octal system is a base-8 number system where each symbol represents 3 digits.

Let us now look into the different ways for conversion of the hexadecimal system to the octal system!

Converting Hexadecimal to Octal the mathematical way through looping:

This involves two steps:

  • Converting the hexadecimal number to decimal.
  • Further converting the decimal number to octal.
print("Enter the Hexadecimal Number: ")
hexa_dec_no = input()
check = 0
dec_num = 0
hexa_dec_no_len = len(hexa_dec_no)
hexa_dec_no_len = hexa_dec_no_len-1
i = 0
while hexa_dec_no_len>=0:
    rem = hexa_dec_no[hexa_dec_no_len]
    if rem>='0' and rem<='9':
        rem = int(rem)
    elif rem>='A' and rem<='F':
        rem = ord(rem)
        rem = rem-55
    elif rem>='a' and rem<='f':
        rem = ord(rem)
        rem = rem-87
    else:
        check = 1
        break
    dec_num = dec_num + (rem * (16 ** i))
    hexa_dec_no_len = hexa_dec_no_len-1
    i = i+1

if check==0:
    i = 0
    octnum = []
    while dec_num!=0:
        rem = dec_num%8
        octnum.insert(i, rem)
        i = i+1
        dec_num = int(dec_num/8)

        
        
    print("\nEquivalent Octal Value is: ")
    i = i-1
    while i>=0:
        print(octnum[i], end="")
        i = i-1
    print()
else:
    print("\nPlease Enter a Valid Input")
Enter the Hexadecimal Number: 
A

Equivalent Octal Value is: 
12

To explain the above procedure in detail;

Part A: Converting the hexadecimal to decimal( the while loop):

  • You will take the hexadecimal number as input from the user and then loop through the number converting each one to its Unicode.
    You can convert the numerical symbols(0-9) simply by using the int() method.
    To convert the alphabets (A-F/a-f) to their Unicode, you can make use of the ord() method.
  • We then multiply each number by its weight and add up all the values to obtain the decimal equivalent.

Part B: Converting the decimal to octal( the if statement):

  • It involves dividing the decimal value by 8 which is the base for the octal system. You must continue the division until the value becomes less than 8. You must also note down the remainders in each step.
  • These remainders, written in last-to-first order give us the octal value. You can implement this using Python lists, as you can see.

Converting hexadecimal to octal by using the in-built methods, int() and oct():

Here, the int() method converts the hexadecimal value to its decimal equivalent. Further, the oct() method converts the received decimal value to octal.

print("Enter Hexadecimal Number: ")
hexa_dec_no = input()
octal_no = int(hexa_dec_no, 16)
octal_no = oct(octal_no)
print("\nEquivalent Octal Value = ", octal_no)
Enter Hexadecimal Number: 
A

Equivalent Octal Value =  0o12

Note: We specify the base i.e., 16 as a parameter in the int() because otherwise, it assumes it to be in the default decimal state, making A an invalid literal.

The same operation can be defined through a single line of code too, as shown below:

def hexa_to_octal(x):
    return oct(int(x, 16))
hexa_to_octal('A')
'0o12'

Well, now that you saw how to convert hexadecimal to octal, you must be curious to know how to perform the operation vice-versa.
You can check out the same here – How to convert octal to hexadecimal in python

Leave a Reply

Your email address will not be published. Required fields are marked *