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