Python program for binary to octal conversion

Binary to Octal Conversion in Python

Here, in this section we will discuss the binary to octal conversion in Python.Binary numbers are also known as bits that represent 0 means FALSE and 1 means TRUE, but usually binary numbers are with base 2 and can represent any number in form of 0 and 1. Whereas Octal numbers expressed with base 8 and can represent any number with 0 to 7. In this python program, we will convert binary numbers to octal numbers using oct() function.

Example : The Binary number 110010101 is equivalent to 645 in Octal base

Binary to octal conversion

Algorithm

  • Step 1:- Start.
  • Step 2:- Take binary numbers from the user.
  • Step 3:- Convert binary to octal using oct() in-built function .
  • Step 4:- Print octal number.
  • Step 5:- End.
binary to octal conversion in Python

Python program for converting binary number to octal number

#take binary number
Bin_num = 0b10111
#convert using oct() function
Oct_num = oct(Bin_num)
#print number
print('Number after conversion is :' + str(Oct_num))
Output:
Number after conversion is :0o27

Another Algorithm

The another way to covert binary number into its octal equivalent is by grouping the binary number into pairs of three and converting into octal.

  • Binary pair “000”  is equivalent to 0 in octal.
  • Binary pair “001”  is equivalent to 1 in octal.
  • Binary pair “010”  is equivalent to 2 in octal.
  • Binary pair “011”  is equivalent to 3 in octal.
  • Binary pair “100”  is equivalent to 4 in octal.
  • Binary pair “101”  is equivalent to 5 in octal.
  • Binary pair “110”  is equivalent to 6 in octal.
  • Binary pair “111”  is equivalent to 7 in octal.

Code in Python Based on above Algorithm

# number and its equivalent octal

def createMap(bin_oct_map):

    bin_oct_map["000"] = '0'

    bin_oct_map["001"] = '1'

    bin_oct_map["010"] = '2'

    bin_oct_map["011"] = '3'

    bin_oct_map["100"] = '4'

    bin_oct_map["101"] = '5'

    bin_oct_map["110"] = '6'

    bin_oct_map["111"] = '7'

# Function to find octal equivalent of binary

def convertBinToOct(bin):

    l = len(bin)

    # length of string before '.'

    t = -1

    if '.' in bin:

        t = bin.index('.')

        len_left = t

    else:

        len_left = l

    # add min 0's in the beginning to make

    # left substring length divisible by 3

    for i in range(1, (3 - len_left % 3) % 3 + 1):

        bin = '0' + bin

    # if decimal point exists

    if (t != -1):

        # length of string after '.'

        len_right = l - len_left - 1

        # add min 0's in the end to make right

        # substring length divisible by 3

        for i in range(1, (3 - len_right % 3) % 3 + 1):

            bin = bin + '0'

    # create dictionary between binary and its

    # equivalent octal code

    bin_oct_map = {}

    createMap(bin_oct_map)

    i = 0

    octal = ""     

    while (True) :         
        # one by one extract from left, substring
        # of size 3 and add its octal code

        octal += bin_oct_map[bin[i:i + 3]]

        i += 3

        if (i == len(bin)):

            break

        # if '.' is encountered add it to result

        if (bin[i] == '.'):

            octal += '.'

            i += 1

    # required octal number

    return octal

# Driver Code

bin = "111000101"

print("Octal number = ",convertBinToOct(bin))
Output :

Octal number = 705