FOLLOW US
DIFFERENENT CONVERSION METHODSFOR HEXA, OCTA AND BINARY | |
In this write-up, we have shared different methods for converting
numbers from one base to the other
| |
| |
| |
| |
| |
def HEX2():
a= int(input('Enter start:\t'))
e= int(input('Enter end:\t'))
d= int(input('Enter base:\t'))
for i in range (a, e):
b = ""
while i != 0:
x = '0123456789ABCDEF'
c = i % d
c1 = x[c]
b = str(c1) + b
i = int(i // d)
print(b)
HEX2()
| |
6. TAKES ANY DECIMAL NUMBER AND CONVERT TO ANY GIVEN BASE
def HEX2():
a= int(input('Enter decimal number: \t'))
d= int(input('Enter expected base: \t'))
b = ""
while a != 0:
x = '0123456789ABCDEF'
c = a % d
c1 = x[c]
b = str(c1) + b
a = int(a // d)
print(b)
HEX2()
| |
| |
8. TAKES JUST THE NUMBER TO CONVERT ANDTHE BASE AND DISPLAY ON GUIimport tkinter as tk from tkinter import* import time def HEX2(): a = int(name_sE.get()) d = int(name_sE2.get()) b = "" while a != 0: x = '0123456789ABCDEF' c = a % d c1 = x[c] b = str(c1) + b a = int(a // d) #print(b) T.delete(1.0, END) T.insert(END, b) def clear_text(): name_sE.delete(0, 'end') name_sE2.delete(0, 'end') return myApp = tk.Tk() myApp.title("Designed by Grace Samson (2017)") myApp.geometry("500x300+0+0") Tops =Frame(myApp, width=300, height = 60, bg ="blue", relief=SUNKEN) Tops.pack(side = TOP) f1 =Frame(myApp, width=500, height = 200, relief=SUNKEN) f1.pack(side = LEFT) f2 =Frame(myApp, width=500, height = 200, relief=SUNKEN) f2.pack(side = RIGHT) d_time = time.asctime(time.localtime(time.time())) lbl1= Label(Tops, font=("arial", 14, "bold"), text= "NUMBER SYSTEM CONVERTER", fg="Steel Blue", bd= 10) lbl1.grid(row=0, column=0) lbl1= Label(Tops, font=("arial", 8, "bold"), text= d_time, fg="Steel Blue", bd= 10) lbl1.grid(row=1, column=0) #signup_bt = Button(Tops, text = 'CONVERSION', # font=("arial", 12, "bold"), width = 15, command =conv_detail) #signup_bt.grid(row=3, column=1) inst2 = Label(f1, font=("arial", 12, "bold"), text ='ENTER NUMBER AND BASE FOR CONVERSION', fg ='RED') inst2.grid(row = 0, column=0, sticky=E) namelbls = Label(f1, font=("arial", 10, "bold"), text ='NUMBER: ') namelbls.grid(row = 3, column=0, sticky=W) name_sE = Entry(f1) name_sE.grid(row = 3, column = 0) #name_sE.bind('<Return>', HEX2) namelbls2 = Label(f1, font=("arial", 10, "bold"), text ='BASE: ') namelbls2.grid(row = 4, column=0, sticky=W) name_sE2 = Entry(f1) name_sE2.grid(row = 4, column = 0) #name_sE2.bind('<Return>', HEX2) conv_bt = Button(f1, text = 'CONVERT', font=("arial", 10, "bold"), width = 10, fg = 'blue', command =HEX2) conv_bt.grid(columnspan=2, sticky=W) reset_bt = Button(f1, text = 'RESET', font=("arial", 10, "bold"), width = 8, fg = 'blue', command =clear_text) reset_bt.grid(columnspan=2, sticky=W) S = Scrollbar(f2) T = Text(f2, height=10, width=50) S.pack(side=RIGHT, fill=Y) T.pack(side=LEFT, fill=Y) S.config(command=T.yview) T.config(yscrollcommand=S.set) ##if __name__ == __"main"__: ## Main() myApp.mainloop() | |
9. TAKES BASE, RANGE AS INPUT AND
CONVERT AT ONCE WHILE DISPLAYING ON GUIimport tkinter as tk
from tkinter import*
import time
def HEX2():
a = int(name_sE.get())
d = int(name_sE2.get())
b = ""
while a != 0:
x = '0123456789ABCDEF'
c = a % d
c1 = x[c]
b = str(c1) + b
a = int(a // d)
#print(b)
T.delete(1.0, END)
T.insert(END, b)
def clear_text():
name_sE.delete(0, 'end')
name_sE2.delete(0, 'end')
return
myApp = tk.Tk()
myApp.title("Designed by Grace Samson (2017)")
myApp.geometry("500x300+0+0")
Tops =Frame(myApp, width=300, height = 60, bg ="blue", relief=SUNKEN)
Tops.pack(side = TOP)
f1 =Frame(myApp, width=500, height = 200, relief=SUNKEN)
f1.pack(side = LEFT)
f2 =Frame(myApp, width=500, height = 200, relief=SUNKEN)
f2.pack(side = RIGHT)
d_time = time.asctime(time.localtime(time.time()))
lbl1= Label(Tops, font=("arial", 14, "bold"), text= "NUMBER SYSTEM CONVERTER", fg="Steel Blue", bd= 10)
lbl1.grid(row=0, column=0)
lbl1= Label(Tops, font=("arial", 8, "bold"), text= d_time, fg="Steel Blue", bd= 10)
lbl1.grid(row=1, column=0)
#signup_bt = Button(Tops, text = 'CONVERSION', font=("arial", 12, "bold"), width = 15, command =conv_detail)
#signup_bt.grid(row=3, column=1)
inst2 = Label(f1, font=("arial", 12, "bold"), text ='ENTER NUMBER AND BASE FOR CONVERSION', fg ='RED')
inst2.grid(row = 0, column=0, sticky=E)
namelbls = Label(f1, font=("arial", 10, "bold"), text ='NUMBER: ')
namelbls.grid(row = 3, column=0, sticky=W)
name_sE = Entry(f1)
name_sE.grid(row = 3, column = 0)
#name_sE.bind('<Return>', HEX2)
namelbls2 = Label(f1, font=("arial", 10, "bold"), text ='BASE: ')
namelbls2.grid(row = 4, column=0, sticky=W)
name_sE2 = Entry(f1)
name_sE2.grid(row = 4, column = 0)
#name_sE2.bind('<Return>', HEX2)
conv_bt = Button(f1, text = 'CONVERT', font=("arial", 10, "bold"), width = 10, fg = 'blue', command =HEX2)
conv_bt.grid(columnspan=2, sticky=W)
reset_bt = Button(f1, text = 'RESET', font=("arial", 10, "bold"), width = 8, fg = 'blue', command =clear_text)
reset_bt.grid(columnspan=2, sticky=W)
S = Scrollbar(f2)
T = Text(f2, height=10, width=50)
S.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)
##if __name__ == __"main"__:
## Main()
myApp.mainloop()
|
10. CONVERTING BACK FROM ANY BASE TO DECIMAL
s = 0
a = (input('enter num: ')).upper()
b = int(input('enter base: '))
for pos, digit in enumerate(a[-1::-1]):
s= (int(digit) * (b**pos)) + s
print(s)
11. CONVERTING BACK FROM BINARY TO DECIMAL
binary = input('enter a number: ')
b = int(input('enter base: '))
decimal = 0
for digit in binary:
decimal = decimal*b + int(digit)
print(decimal)
12. CONVERTING BACK FROM ANY BASE TO DECIMAL
def todec():
👍
c = int(input('Enter base of the number to convert to decimal:\t'))
a = (input('Then enter the number:\t ')).upper()
b = list(a)
s = 0
x = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
for pos, digit in enumerate(b[-1::-1]):
y = x.index(digit)
if int(y)/c >= 1:
print('Invalid input!!!')
break
s = (int(y) * (c**pos)) + s
return (s)
todec()
Press h to open a hovercard with more details.
No comments:
Post a Comment