Skip to content

Commit 669a52e

Browse files
committed
Added number-conversion script
1 parent 08b7bdd commit 669a52e

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed
4.08 KB
Loading
3.95 KB
Loading
3.73 KB
Loading
4 KB
Loading

Number Conversion/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Number Conversion
2+
3+
Short description of package/script
4+
5+
- With this python script one can convert a user input number from one base to other.By Default conversion takes place from decimal to binary.Specify the FROM and TO parameters from the following:
6+
-d->decimal,
7+
-b->binary,
8+
-x->hexadecimal,
9+
-o->octal
10+
11+
## Setup instructions
12+
13+
- run the converter_script.py file in terminal
14+
15+
## Output
16+
17+
Display images/gifs/videos of output/result of your script so that users can visualize it
18+
![hexa_to_binary](Output/hexa_to_binary.png)
19+
![octal_to_binary](Output/octal_to_binary.png)
20+
![octal_to_hexa](Output/octal_to_hexa.png)
21+
![octal_to_decimal](Output/octal_to_decimal.png)
22+
23+
## Author(s)
24+
25+
- Samarth Chaplot
26+

Number Conversion/converter_script.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)