This document contains code for a program that converts numbers between different number bases. It defines functions to verify if a number is valid in a given base, convert a number from base 10 to another base, convert a number from another base to base 10, and functions to convert numbers and clear fields when buttons are clicked.
This document contains code for a program that converts numbers between different number bases. It defines functions to verify if a number is valid in a given base, convert a number from base 10 to another base, convert a number from another base to base 10, and functions to convert numbers and clear fields when buttons are clicked.
2 from PyQt5.QtWidgets import * 3 def verifbase(x,a): 4 re=True 5 if x=="": 6 return False 7 m=str(int(a)-1) 8 if int(a)<=10: 9 10 for i in range(len(x)): 11 if not "0"<=x[i]<=m: 12 re=False 13 elif int(a)==11: 14 for i in range(len(x)): 15 if not "0"<=x[i]<"9" and x[i]!="A": 16 re=False 17 elif int(a)==12: 18 for i in range(len(x)): 19 if not "0"<=x[i]<"9" and not "A"<=x[i]<="B": 20 re=False 21 elif int(a)==13: 22 23 for i in range(len(x)): 24 if not "0"<=x[i]<"9" and not "A"<=x[i]<="C": 25 re=False 26 elif int(a)==14: 27 for i in range(len(x)): 28 if not "0"<=x[i]<"9" and not "A"<=x[i]<="D": 29 re=False 30 elif int(a)==15: 31 for i in range(len(x)): 32 if not "0"<=x[i]<"9" and not "A"<=x[i]<="E": 33 re=False 34 elif int(a)==16: 35 for i in range(len(x)): 36 if not "0"<=x[i]<"9" and not "A"<=x[i]<="F": 37 re=False 38 return re 39 def conv10_b(x,b): 40 ch="" 41 x=int(x) 42 b=int(b) 43 if b<=10: 44 while x!=0: 45 a=x%b 46 ch=str(a)+ch 47 x=x//b 48 else : 49 while x!=0: 50 a=x%b 51 if a<=9: 52 ch=str(a)+ch 53 else : 54 ch=chr(a+55)+ch 55 x=x//b 56 return ch 57 def convb_10(x,a): 58 s=0 59 a=int(a) 60 p=1 61 for i in range(len(x)-1,-1,-1): 62 if "0"<=x[i]<="9": 63 64 s=s+int(x[i])*p 65 p=p*a 66 else : 67 s=s+(ord(x[i])-55)*p 68 p=p*a 69 file:///C:/Users/Msi/AppData/Roaming/Thonny/temp/thonny_qf53489l.html 1/2 27/04/2024 17:59 conv.py 70 return str(s) 71 72 def convertir(): 73 x=w.lineEdit.text() 74 a=w.comboBox.currentText() 75 b=w.comboBox_2.currentText() 76 if not verifbase(x,a): 77 QMessageBox.critical(w,"ERREURE ","Nombre non valide !!!") 78 w.lineEdit.setText("") 79 else : 80 if a==b: 81 w.lineEdit_2.setText(x) 82 elif a=="10" : 83 ch=conv10_b(x,b) 84 w.lineEdit_2.setText(ch) 85 elif b=="10": 86 ch=convb_10(x,a) 87 w.lineEdit_2.setText(ch) 88 89 else: 90 ch1=convb_10(x,a) 91 ch2=conv10_b(ch1,b) 92 w.lineEdit_2.setText(ch2) 93 def effacer(): 94 w.lineEdit.clear() 95 w.lineEdit_2.clear() 96 w.comboBox.setCurrentIndex(0) 97 w.comboBox_2.setCurrentIndex(0) 98 def fermer(): 99 w.close() 100 101 app=QApplication([]) 102 w=loadUi("conversion.ui") 103 w.show() 104 w.pushButton.clicked.connect(convertir) 105 w.pushButton_2.clicked.connect(effacer) 106 w.pushButton_3.clicked.connect(fermer) 107 app.exec_()