Skip to content

Commit b851c91

Browse files
Added Number Base Converter Project
This program converts a number in any one number base system into the remaining formats.
1 parent 54f5322 commit b851c91

File tree

7 files changed

+644
-0
lines changed

7 files changed

+644
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Number Base Converter
2+
3+
### About
4+
5+
There are various types of number systems in mathematics. The four most common number system types are:
6+
+ Decimal number system (Base- 10)
7+
+ Binary number system (Base- 2)
8+
+ Octal number system (Base-8)
9+
+ Hexadecimal number system (Base- 16)
10+
11+
This program takes input as a number in any one number base system and converts it into the remaining formats.
12+
13+
### Requirements
14+
+ tkinter
15+
16+
### To run this script
17+
python main.py
18+
19+
### Output Screenshot
20+
![](output.png "Sample Output")
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
from tobinary import decimalToBinary, octalToBinary, hexToBinary
2+
from tooctal import decimalToOctal, binaryToOctal, hexToOctal
3+
from tohex import decimalToHex, binaryToHex, octalToHex
4+
from todecimal import binaryToDecimal, octalToDecimal, hexToDecimal
5+
6+
from tkinter import *
7+
from tkinter import messagebox
8+
9+
root = Tk()
10+
root.title("Number Base Conversion")
11+
root.state('zoomed')
12+
root.configure(background="#011")
13+
14+
# variables-----------------------------------------------------------------------------------------------------------
15+
global decimal_ip, binary_ip, octal_ip, hexadecimal_ip
16+
decimal_ip = StringVar()
17+
binary_ip = StringVar()
18+
octal_ip = StringVar()
19+
hexadecimal_ip = StringVar()
20+
21+
# functions-----------------------------------------------------------------------------------------------------------
22+
def convert():
23+
decimal = decimal_ip.get()
24+
binary = binary_ip.get()
25+
octal = octal_ip.get()
26+
hex = hexadecimal_ip.get()
27+
28+
result = False
29+
30+
if decimal:
31+
try:
32+
float(decimal)
33+
result = True
34+
except:
35+
messagebox.showerror("Error", "Invalid Decimal Value", parent=root)
36+
decimal_ip.set('')
37+
38+
if result:
39+
b = decimalToBinary(decimal)
40+
o = decimalToOctal(decimal)
41+
h = decimalToHex(decimal)
42+
43+
binary_ip.set(b)
44+
octal_ip.set(o)
45+
hexadecimal_ip.set(h)
46+
47+
if binary:
48+
s = set(binary)
49+
if '.' in s or '0' in s or '1' in s:
50+
result = True
51+
else:
52+
messagebox.showerror("Error", "Invalid Binary Value", parent=root)
53+
binary_ip.set('')
54+
55+
if result:
56+
try:
57+
d = binaryToDecimal(binary)
58+
o = binaryToOctal(binary)
59+
h = binaryToHex(binary)
60+
61+
decimal_ip.set(d)
62+
octal_ip.set(o)
63+
hexadecimal_ip.set(h)
64+
65+
except:
66+
messagebox.showerror("Error", "Invalid Binary Value", parent=root)
67+
binary_ip.set('')
68+
69+
if octal:
70+
try:
71+
o = float(octal)
72+
if '8' in str(o) or '9' in str(o):
73+
messagebox.showerror("Error", "Invalid Octal Value", parent=root)
74+
octal_ip.set('')
75+
else:
76+
result = True
77+
except:
78+
messagebox.showerror("Error", "Invalid Octal Value", parent=root)
79+
octal_ip.set('')
80+
81+
if result:
82+
try:
83+
d = octalToDecimal(octal)
84+
b = octalToBinary(octal)
85+
h = octalToHex(octal)
86+
87+
decimal_ip.set(d)
88+
binary_ip.set(b)
89+
hexadecimal_ip.set(h)
90+
91+
except:
92+
messagebox.showerror("Error", "Invalid Octal Value", parent=root)
93+
octal_ip.set('')
94+
95+
if hex:
96+
result = True
97+
for h in hex.upper():
98+
if h == '.':
99+
pass
100+
elif ((h < '0' or h > '9') and (h < 'A' or h > 'F')):
101+
result = False
102+
break
103+
104+
if result:
105+
try:
106+
d = hexToDecimal(hex)
107+
b = hexToBinary(hex)
108+
o = hexToOctal(hex)
109+
110+
decimal_ip.set(d)
111+
binary_ip.set(b)
112+
octal_ip.set(o)
113+
114+
except:
115+
messagebox.showerror("Error", "Invalid Hexadecimal Value", parent=root)
116+
hexadecimal_ip.set('')
117+
118+
else:
119+
messagebox.showerror("Error", "Invalid Hexadecimal Value", parent=root)
120+
hexadecimal_ip.set('')
121+
122+
def clear():
123+
decimal_ip.set('')
124+
binary_ip.set('')
125+
octal_ip.set('')
126+
hexadecimal_ip.set('')
127+
128+
# widgets-------------------------------------------------------------------------------------------------------------
129+
title = Label(
130+
root,
131+
text="Number Base Conversion",
132+
fg="#0c0",
133+
bg="#011",
134+
font=("verdana", 30, "bold"),
135+
pady=20,
136+
).pack(fill=X)
137+
138+
F1 = LabelFrame(
139+
root,
140+
bd=0,
141+
font=("verdana", 15, "bold"),
142+
bg="#011",
143+
)
144+
F1.place(relx=0.5, rely=0.5, anchor=CENTER)
145+
146+
decimal_lbl = Label(
147+
F1,
148+
text="Decimal No. :",
149+
font=("verdana", 20, "bold"),
150+
bg="#011",
151+
fg="#fff",
152+
).grid(sticky=E, row=0, column=0, padx=20, pady=20, ipadx=10, ipady=10)
153+
154+
decimal_txt = Entry(
155+
F1,
156+
width=25,
157+
textvariable=decimal_ip,
158+
font="arial 15",
159+
bd=7,
160+
relief=SUNKEN
161+
).grid(row=0, column=1, padx=20, pady=20, ipadx=10, ipady=10)
162+
163+
binary_lbl = Label(
164+
F1,
165+
text="Binary No. :",
166+
font=("verdana", 20, "bold"),
167+
bg="#011",
168+
fg="#fff",
169+
).grid(sticky=E, row=1, column=0, padx=20, pady=20, ipadx=10, ipady=10)
170+
171+
binary_txt = Entry(
172+
F1,
173+
width=25,
174+
textvariable=binary_ip,
175+
font="arial 15",
176+
bd=7,
177+
relief=SUNKEN
178+
).grid(row=1, column=1, padx=20, pady=20, ipadx=10, ipady=10)
179+
180+
octal_lbl = Label(
181+
F1,
182+
text="Octal No. :",
183+
font=("verdana", 20, "bold"),
184+
bg="#011",
185+
fg="#fff",
186+
).grid(sticky=E, row=2, column=0, padx=20, pady=20, ipadx=10, ipady=10)
187+
188+
octal_txt = Entry(
189+
F1,
190+
width=25,
191+
textvariable=octal_ip,
192+
font="arial 15",
193+
bd=7,
194+
relief=SUNKEN
195+
).grid(row=2, column=1, padx=20, pady=20, ipadx=10, ipady=10)
196+
197+
hexadecimal_lbl = Label(
198+
F1,
199+
text="Hexadecimal No. :",
200+
font=("verdana", 20, "bold"),
201+
bg="#011",
202+
fg="#fff",
203+
).grid(sticky=E, row=3, column=0, padx=20, pady=20, ipadx=10, ipady=10)
204+
205+
hexadecimal_txt = Entry(
206+
F1,
207+
width=25,
208+
textvariable=hexadecimal_ip,
209+
font="arial 15",
210+
bd=7,
211+
relief=SUNKEN
212+
).grid(row=3, column=1, padx=20, pady=20, ipadx=10, ipady=10)
213+
214+
convert_btn = Button(
215+
F1,
216+
text="Convert",
217+
command=convert,
218+
width=10,
219+
bd=7,
220+
font="verdana 20 bold",
221+
).grid(row=1, column=4, columnspan=2, padx=20, pady=20, ipadx=5, ipady=5)
222+
223+
clear_btn = Button(
224+
F1,
225+
text="Clear",
226+
command=clear,
227+
width=10,
228+
bd=7,
229+
font="verdana 20 bold",
230+
).grid(row=2, column=4, columnspan=2, padx=20, pady=20, ipadx=5, ipady=5)
231+
232+
quit_btn = Button(
233+
F1,
234+
text="Quit",
235+
command=root.quit,
236+
width=10,
237+
bd=7,
238+
font="verdana 20 bold",
239+
).grid(row=3, column=4, columnspan=2, padx=20, pady=20, ipadx=5, ipady=5)
240+
241+
root.mainloop()
182 KB
Loading
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
def decimalToBinary(decimal):
2+
decimal = str(decimal)
3+
if '.' in decimal: integral, fractional = decimal.split('.')
4+
else:
5+
integral = decimal
6+
fractional = 0
7+
8+
binaryNo = ''
9+
if integral:
10+
integral = int(integral)
11+
while integral > 0:
12+
binaryNo += str(integral % 2)
13+
integral = integral // 2
14+
15+
binaryNo = binaryNo[::-1]
16+
17+
if fractional:
18+
fractional = '0.' + fractional
19+
binaryNo += '.'
20+
for i in range(20):
21+
prod = float(fractional) * 2
22+
num = int(prod)
23+
fractional = prod - num
24+
binaryNo += str(num)
25+
26+
return binaryNo
27+
28+
def octalToBinary(octal):
29+
octToBin = {
30+
'0': '000',
31+
'1': '001',
32+
'2': '010',
33+
'3': '011',
34+
'4': '100',
35+
'5': '101',
36+
'6': '110',
37+
'7': '111',
38+
'.': '.'
39+
}
40+
41+
binary = ''
42+
for o in str(octal):
43+
try:
44+
binary += octToBin[o]
45+
except (KeyError, ValueError):
46+
return 'Invalid Input'
47+
return binary
48+
49+
def hexToBinary(hex):
50+
hexToBin = {
51+
'0': '0000',
52+
'1': '0001',
53+
'2': '0010',
54+
'3': '0011',
55+
'4': '0100',
56+
'5': '0101',
57+
'6': '0110',
58+
'7': '0111',
59+
'8': '1000',
60+
'9': '1001',
61+
'a': '1010',
62+
'b': '1011',
63+
'c': '1100',
64+
'd': '1101',
65+
'e': '1110',
66+
'f': '1111',
67+
'.': '.'
68+
}
69+
70+
binary = ''
71+
for h in str(hex):
72+
try:
73+
if h.isalpha():
74+
h = h.lower()
75+
binary += hexToBin[h]
76+
except (KeyError, ValueError):
77+
return 'Invalid Input'
78+
return binary

0 commit comments

Comments
 (0)