Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 18e7cba

Browse files
Merge pull request avinashkranjan#2367 from sam-arth07/Number-Conversion
Added number-conversion script
2 parents 7ed356a + 961dd6c commit 18e7cba

File tree

6 files changed

+101
-0
lines changed

6 files changed

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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /