Python program to convert Octal to Decimal, Binary, Hexadecimal














































Python program to convert Octal to Decimal, Binary, Hexadecimal



DESCRIPTION: The below code converts Octal to Decimal, Binary, Hexadecimal.
The user has to enter a choice, and then the octal value for converting it into its respective 
choice.
Enter 1 for Octal to Decimal conversion
Enter 2 for Octal to Binary conversion
Enter 3 for Octal to Hexadecimal conversion



#The class is converting octal numbers
class Octal():
    #Constructor takes the Choice
    def __init__(self, choice):
        self.choice = choice
    
    def octal_conversion(self):
        #If entered choice is 1, 2, or 3
        if(self.choice >=1 and self.choice <= 3):
            #Enter octal number
            octal = input("Enter number in octal format: ")

            #Octal to Decimal
            if self.choice == 1:
                decimal = str(int(octal,8))
                print(octal, "in Decimal is ", decimal)

            #Octal to Binary
            elif self.choice == 2:
                decimal = str(int(octal, 8))
                decm = int(decimal)
                print(octal, "in Binary is ", bin(decm))
                
            #Octal to Hexadecimal    
            elif self.choice == 3:
                decimal = str(int(octal, 8))
                decm = int(decimal)
                print(octal, "in HexaDecimal is ", hex(decm))
        #If user wants to exit
        elif self.choice == 4:
            exit()
        #User enters an invalid choice
        else:
            print("Invalid Entry!!")
         

def main():
    #Following choices can be entered
    #for converting Octal to Decimal, Binary, Hexadecimal
    print("Type")
    print("1 for Octal to Decimal")
    print("2 for Octal to Binary")
    print("3 for Octal to Hexadecimal")
    print("4 for exit")
    #Enter a valid choice
    choice = int(input("Enter your Choice: "))
    #Object created for the class Octal
    obj_Octal = Octal(choice)
    obj_Octal.octal_conversion()

if __name__ == "__main__":
    main()

OUTPUT:
Test case 1:
	Type
        1 for Octal to Decimal
        2 for Octal to Binary
        3 for Octal to Hexadecimal
        4 for exit
        Enter your Choice: 1
        Enter number in octal format: 2567
        2567 in Decimal is  1399
Test case 2:
        Type
        1 for Octal to Decimal
        2 for Octal to Binary
        3 for Octal to Hexadecimal
        4 for exit
        Enter your Choice: 2
        Enter number in octal format: 2567
        2567 in Binary is  0b10101110111
Test case 3:
        Type
        1 for Octal to Decimal
        2 for Octal to Binary
        3 for Octal to Hexadecimal
        4 for exit
        Enter your Choice: 3
        Enter number in octal format: 2567
        2567 in Hexadecimal is  0x577
Test case 4:
        Type
        1 for Octal to Decimal
        2 for Octal to Binary
        3 for Octal to Hexadecimal
        4 for exit
        Enter your Choice: 7
        Invalid Entry!!

More Articles of Anjani Dubey:

Name Views Likes
Python program to check if a Binary tree is height balanced or not 902 16
Python program to convert BST into MinHeap 447 18
Python program to delete an element into AVL Tree 870 19
Python program to insert an element in AVL Tree 473 27
Python program to delete an element in Binary search tree 442 18
Python program to insert and find an element in Binary Search Tree 513 18
Python program to insert an element in the Binary Tree 761 18
Python program to find height of the Binary Search Tree(BST) 574 11
Python program to combine two sorted lists and make another sorted list 418 16
Python program to convert multiple integers into single integer 727 22
Python program to convert Nested list into a flat list 527 17
Python program to convert tuple to a string 601 26
Python program for traversing the Tree in PreOrder, PostOrder and InOrder 741 13
Python program to find hash of a string 379 28
Python program for Counting sort Algorithm 307 13
Python program for Insertion sort Algorithm 499 23
Python program for Bubble Sort algorithm 384 12
Python program for UNIX domain socket UDP client 341 15
Python UNIX domain socket UDP server 422 24
Python program for Binary Search Tree 438 20
Python UNIX domain socket TCP Client 316 16
Python UNIX domain socket TCP Server 383 16
Python program to convert POS to SOP 341 15
Advantages of UNIX domain socket over Normal sockets 628 24
Python program for Merge Sort Algorithm 382 21
Python program to print duplicate characters and count from a string 1535 20
Python program to find all pairs of an integer array whose sum is equal to a given number 1192 18
Python program to find all Permutations of a string 347 11
Python program to reverse the string using recursion 444 15
Python program to check two strings are anagrams of each other 712 22
Python program to convert Hexadecimal to Decimal, Binary, Octal 1763 27
Python program to convert Octal to Decimal, Binary, Hexadecimal 1850 28
Python program to Shuffle deck of cards 907 18
Python program for One Dimensional Array 333 23
Python program to merge mails 334 19
Python program to display powers of 2 380 20
Python program to find numbers Divisible by Another number 1007 24
Python program to print strings 297 14
Python program to find Average of numbers 307 23
Python program to find the solutions for a Quadratic equation 772 18
Python program to generate Armstrong numbers for a specific range 253 17
Python program to calculate perimeter of Triangle 379 23
Python program to calculate perimeter of a Rectangle 303 18
Python program to calculate the factors of a number 255 11
Python program to calculate the Square root of a number 478 19
Python program to calculate the area of Rectangle 322 17
Python program to print the Floyd Triangle 278 15
Python program to find the sum of natural number using Recursion 326 17
Python program to find the Fibonacci sequence recursion 426 30
Python Program to check whether a number is Palindrome or not 498 11
Program to check whether a number is Palindrome or not 456 17
Program to find average of numbers 318 16
Program to solve the Quadratic Equation 358 19
Program to generate the Armstrong numbers for a specific range 307 21
Program to calculate the perimeter of triangle 342 22
Program to calculate the perimeter of rectangle 479 14
Program to Display the Fibonacci series using Recursion 241 14
Program to find factors of a number 275 18
Program to calculate Square root of a number 244 18
Program to calculate area of Rectangle 303 25
Python Program for calculating the Area of Triangle 294 22
Program to print the Floyd Triangle 268 20
Python Program to calculate the Grade of the Student 5961 21
Program to calculate sum of natural numbers using Recursion 254 18

Comments