|
| 1 | +import sys |
| 2 | +class Definations: |
| 3 | + '''Contains the actual logic for base conversion. |
| 4 | +
|
| 5 | + Parent class to Converter class |
| 6 | + ''' |
| 7 | + def __init__(self,number): |
| 8 | + self.number = number |
| 9 | + def Decimal_to_Binary(self): |
| 10 | + return bin(int(self.number))[2:] |
| 11 | + def Binary_to_Decimal(self): |
| 12 | + return int(str(self.number),2) |
| 13 | + def Decimal_to_Octal(self): |
| 14 | + return oct(int(self.number))[2:] |
| 15 | + def Octal_to_Decimal(self): |
| 16 | + return int(str(self.number),8) |
| 17 | + def Decimal_to_Hexadecimal(self): |
| 18 | + return hex(self.number)[2:] |
| 19 | + def Hexadecimal_to_Decimal(self): |
| 20 | + return int(str(self.number),16) |
| 21 | + def Hexadecimal_to_Binary(self): |
| 22 | + num = int(str(self.number),16) |
| 23 | + return bin(num)[2:] |
| 24 | + def Hexadecimal_to_Octal(self): |
| 25 | + num = int(str(self.number),16) |
| 26 | + return oct(num)[2:] |
| 27 | + def Binary_to_Octal(self): |
| 28 | + num = int(str(self.number),2) |
| 29 | + return oct(num)[2:] |
| 30 | + def Binary_to_Hexadecimal(self): |
| 31 | + num = int(str(self.number),2) |
| 32 | + return hex(num)[2:] |
| 33 | + def Octal_to_Hexadecimal(self): |
| 34 | + num = int(str(self.number),8) |
| 35 | + return hex(num)[2:] |
| 36 | + def Octal_to_Binary(self): |
| 37 | + num = int(str(self.number),8) |
| 38 | + return bin(num)[2:] |
| 39 | + |
| 40 | +class Converter(Definations): |
| 41 | + '''Inherits the Definations Class and converts the number based on user input''' |
| 42 | + def __init__(self,number): |
| 43 | + super().__init__(number) |
| 44 | + |
| 45 | + def helper(self, func_name): |
| 46 | + return getattr(Definations,func_name)(self) |
| 47 | + |
| 48 | + def convert(self,FROM="d",TO="b"): |
| 49 | + ''' |
| 50 | + By Default conversion takes place from decimal to binary. |
| 51 | + specify the FROM and TO parameters from the following: |
| 52 | + d-decimal, |
| 53 | + b-binary, |
| 54 | + x-hexadecimal, |
| 55 | + o-octal |
| 56 | + ''' |
| 57 | + bases = {'d':"Decimal",'b':"Binary",'x':'Hexadecimal','o':"Octal"} |
| 58 | + to_call_function = bases[FROM] + '_to_' + bases[TO] |
| 59 | + return f"\n{self.number} in {bases[FROM]} = {self.helper(to_call_function)} in {bases[TO]}" |
| 60 | + |
| 61 | +def header_decoration(): |
| 62 | + print(''' |
| 63 | + -------------- WELCOME TO NUMBER CONVERTER -------------- |
| 64 | + -------------- --------------------------- -------------- |
| 65 | + ''') |
| 66 | +def footer_decoration(): |
| 67 | + print(''' |
| 68 | + -------------- --------------------------- ----------- |
| 69 | + ''') |
| 70 | + |
| 71 | +if __name__=='__main__': |
| 72 | + header_decoration() |
| 73 | + Num = Converter(input("Enter number = ")) |
| 74 | + print(Num.convert(input("From = "),input("To = "))) |
| 75 | + footer_decoration() |
0 commit comments