diff --git a/CONVERSION SCRIPTS/Number Base Converter/README.md b/CONVERSION SCRIPTS/Number Base Converter/README.md new file mode 100644 index 00000000..b8c2cbee --- /dev/null +++ b/CONVERSION SCRIPTS/Number Base Converter/README.md @@ -0,0 +1,20 @@ +# Number Base Converter + +### About + +There are various types of number systems in mathematics. The four most common number system types are: + + Decimal number system (Base- 10) + + Binary number system (Base- 2) + + Octal number system (Base-8) + + Hexadecimal number system (Base- 16) + +This program takes input as a number in any one number base system and converts it into the remaining formats. + +### Requirements + + tkinter + +### To run this script +python main.py + +### Output Screenshot +![](output.png "Sample Output") \ No newline at end of file diff --git a/CONVERSION SCRIPTS/Number Base Converter/main.py b/CONVERSION SCRIPTS/Number Base Converter/main.py new file mode 100644 index 00000000..450756c5 --- /dev/null +++ b/CONVERSION SCRIPTS/Number Base Converter/main.py @@ -0,0 +1,241 @@ +from tobinary import decimalToBinary, octalToBinary, hexToBinary +from tooctal import decimalToOctal, binaryToOctal, hexToOctal +from tohex import decimalToHex, binaryToHex, octalToHex +from todecimal import binaryToDecimal, octalToDecimal, hexToDecimal + +from tkinter import * +from tkinter import messagebox + +root = Tk() +root.title("Number Base Conversion") +root.state('zoomed') +root.configure(background="#011") + +# variables----------------------------------------------------------------------------------------------------------- +global decimal_ip, binary_ip, octal_ip, hexadecimal_ip +decimal_ip = StringVar() +binary_ip = StringVar() +octal_ip = StringVar() +hexadecimal_ip = StringVar() + +# functions----------------------------------------------------------------------------------------------------------- +def convert(): + decimal = decimal_ip.get() + binary = binary_ip.get() + octal = octal_ip.get() + hex = hexadecimal_ip.get() + + result = False + + if decimal: + try: + float(decimal) + result = True + except: + messagebox.showerror("Error", "Invalid Decimal Value", parent=root) + decimal_ip.set('') + + if result: + b = decimalToBinary(decimal) + o = decimalToOctal(decimal) + h = decimalToHex(decimal) + + binary_ip.set(b) + octal_ip.set(o) + hexadecimal_ip.set(h) + + if binary: + s = set(binary) + if '.' in s or '0' in s or '1' in s: + result = True + else: + messagebox.showerror("Error", "Invalid Binary Value", parent=root) + binary_ip.set('') + + if result: + try: + d = binaryToDecimal(binary) + o = binaryToOctal(binary) + h = binaryToHex(binary) + + decimal_ip.set(d) + octal_ip.set(o) + hexadecimal_ip.set(h) + + except: + messagebox.showerror("Error", "Invalid Binary Value", parent=root) + binary_ip.set('') + + if octal: + try: + o = float(octal) + if '8' in str(o) or '9' in str(o): + messagebox.showerror("Error", "Invalid Octal Value", parent=root) + octal_ip.set('') + else: + result = True + except: + messagebox.showerror("Error", "Invalid Octal Value", parent=root) + octal_ip.set('') + + if result: + try: + d = octalToDecimal(octal) + b = octalToBinary(octal) + h = octalToHex(octal) + + decimal_ip.set(d) + binary_ip.set(b) + hexadecimal_ip.set(h) + + except: + messagebox.showerror("Error", "Invalid Octal Value", parent=root) + octal_ip.set('') + + if hex: + result = True + for h in hex.upper(): + if h == '.': + pass + elif ((h < '0' or h > '9') and (h < 'A' or h > 'F')): + result = False + break + + if result: + try: + d = hexToDecimal(hex) + b = hexToBinary(hex) + o = hexToOctal(hex) + + decimal_ip.set(d) + binary_ip.set(b) + octal_ip.set(o) + + except: + messagebox.showerror("Error", "Invalid Hexadecimal Value", parent=root) + hexadecimal_ip.set('') + + else: + messagebox.showerror("Error", "Invalid Hexadecimal Value", parent=root) + hexadecimal_ip.set('') + +def clear(): + decimal_ip.set('') + binary_ip.set('') + octal_ip.set('') + hexadecimal_ip.set('') + +# widgets------------------------------------------------------------------------------------------------------------- +title = Label( + root, + text="Number Base Conversion", + fg="#0c0", + bg="#011", + font=("verdana", 30, "bold"), + pady=20, +).pack(fill=X) + +F1 = LabelFrame( + root, + bd=0, + font=("verdana", 15, "bold"), + bg="#011", +) +F1.place(relx=0.5, rely=0.5, anchor=CENTER) + +decimal_lbl = Label( + F1, + text="Decimal No. :", + font=("verdana", 20, "bold"), + bg="#011", + fg="#fff", +).grid(sticky=E, row=0, column=0, padx=20, pady=20, ipadx=10, ipady=10) + +decimal_txt = Entry( + F1, + width=25, + textvariable=decimal_ip, + font="arial 15", + bd=7, + relief=SUNKEN +).grid(row=0, column=1, padx=20, pady=20, ipadx=10, ipady=10) + +binary_lbl = Label( + F1, + text="Binary No. :", + font=("verdana", 20, "bold"), + bg="#011", + fg="#fff", +).grid(sticky=E, row=1, column=0, padx=20, pady=20, ipadx=10, ipady=10) + +binary_txt = Entry( + F1, + width=25, + textvariable=binary_ip, + font="arial 15", + bd=7, + relief=SUNKEN +).grid(row=1, column=1, padx=20, pady=20, ipadx=10, ipady=10) + +octal_lbl = Label( + F1, + text="Octal No. :", + font=("verdana", 20, "bold"), + bg="#011", + fg="#fff", +).grid(sticky=E, row=2, column=0, padx=20, pady=20, ipadx=10, ipady=10) + +octal_txt = Entry( + F1, + width=25, + textvariable=octal_ip, + font="arial 15", + bd=7, + relief=SUNKEN +).grid(row=2, column=1, padx=20, pady=20, ipadx=10, ipady=10) + +hexadecimal_lbl = Label( + F1, + text="Hexadecimal No. :", + font=("verdana", 20, "bold"), + bg="#011", + fg="#fff", +).grid(sticky=E, row=3, column=0, padx=20, pady=20, ipadx=10, ipady=10) + +hexadecimal_txt = Entry( + F1, + width=25, + textvariable=hexadecimal_ip, + font="arial 15", + bd=7, + relief=SUNKEN +).grid(row=3, column=1, padx=20, pady=20, ipadx=10, ipady=10) + +convert_btn = Button( + F1, + text="Convert", + command=convert, + width=10, + bd=7, + font="verdana 20 bold", +).grid(row=1, column=4, columnspan=2, padx=20, pady=20, ipadx=5, ipady=5) + +clear_btn = Button( + F1, + text="Clear", + command=clear, + width=10, + bd=7, + font="verdana 20 bold", +).grid(row=2, column=4, columnspan=2, padx=20, pady=20, ipadx=5, ipady=5) + +quit_btn = Button( + F1, + text="Quit", + command=root.quit, + width=10, + bd=7, + font="verdana 20 bold", +).grid(row=3, column=4, columnspan=2, padx=20, pady=20, ipadx=5, ipady=5) + +root.mainloop() \ No newline at end of file diff --git a/CONVERSION SCRIPTS/Number Base Converter/output.png b/CONVERSION SCRIPTS/Number Base Converter/output.png new file mode 100644 index 00000000..169553d2 Binary files /dev/null and b/CONVERSION SCRIPTS/Number Base Converter/output.png differ diff --git a/CONVERSION SCRIPTS/Number Base Converter/tobinary.py b/CONVERSION SCRIPTS/Number Base Converter/tobinary.py new file mode 100644 index 00000000..001951d6 --- /dev/null +++ b/CONVERSION SCRIPTS/Number Base Converter/tobinary.py @@ -0,0 +1,78 @@ +def decimalToBinary(decimal): + decimal = str(decimal) + if '.' in decimal: integral, fractional = decimal.split('.') + else: + integral = decimal + fractional = 0 + + binaryNo = '' + if integral: + integral = int(integral) + while integral > 0: + binaryNo += str(integral % 2) + integral = integral // 2 + + binaryNo = binaryNo[::-1] + + if fractional: + fractional = '0.' + fractional + binaryNo += '.' + for i in range(20): + prod = float(fractional) * 2 + num = int(prod) + fractional = prod - num + binaryNo += str(num) + + return binaryNo + +def octalToBinary(octal): + octToBin = { + '0': '000', + '1': '001', + '2': '010', + '3': '011', + '4': '100', + '5': '101', + '6': '110', + '7': '111', + '.': '.' + } + + binary = '' + for o in str(octal): + try: + binary += octToBin[o] + except (KeyError, ValueError): + return 'Invalid Input' + return binary + +def hexToBinary(hex): + hexToBin = { + '0': '0000', + '1': '0001', + '2': '0010', + '3': '0011', + '4': '0100', + '5': '0101', + '6': '0110', + '7': '0111', + '8': '1000', + '9': '1001', + 'a': '1010', + 'b': '1011', + 'c': '1100', + 'd': '1101', + 'e': '1110', + 'f': '1111', + '.': '.' + } + + binary = '' + for h in str(hex): + try: + if h.isalpha(): + h = h.lower() + binary += hexToBin[h] + except (KeyError, ValueError): + return 'Invalid Input' + return binary \ No newline at end of file diff --git a/CONVERSION SCRIPTS/Number Base Converter/todecimal.py b/CONVERSION SCRIPTS/Number Base Converter/todecimal.py new file mode 100644 index 00000000..7de5e69a --- /dev/null +++ b/CONVERSION SCRIPTS/Number Base Converter/todecimal.py @@ -0,0 +1,77 @@ +def binaryToDecimal(binary): + binary = str(binary) + if '.' in binary: integral, fractional = binary.split('.') + else: + integral = binary + fractional = 0 + + if integral: + integral = integral[::-1] + + decimal = 0 + for i in range(len(integral)): + decimal += int(integral[i]) * (2**i) + + if fractional: + for i in range(len(fractional)): + decimal += int(fractional[i]) * (1/(2**(1+i))) + + return decimal + +def octalToDecimal(octal): + octal = str(octal) + if '.' in octal: integral, fractional = octal.split('.') + else: + integral = octal + fractional = 0 + + if integral: + integral = integral[::-1] + + decimal = 0 + for i in range(len(integral)): + decimal += int(integral[i]) * (8**i) + + if fractional: + for i in range(len(fractional)): + decimal += int(fractional[i]) * (1/(8**(1+i))) + return decimal + +def hexToDecimal(hex): + hex = str(hex) + + hex_nos = { + 'a': '10', + 'b': '11', + 'c': '12', + 'd': '13', + 'e': '14', + 'f': '15', + } + + if '.' in hex: integral, fractional = hex.split('.') + else: + integral = hex + fractional = 0 + + if integral: + integral = integral[::-1] + + decimal = 0 + for i in range(len(integral)): + if integral[i].isalpha(): + j = hex_nos[integral[i]] + decimal += int(j) * (16**i) + else: + decimal += int(integral[i]) * (16**i) + + + if fractional: + for i in range(len(fractional)): + if fractional[i].isalpha(): + j = hex_nos[fractional[i]] + decimal += int(j) * (1/(16**(1+i))) + else: + decimal += int(fractional[i]) * (1/(16**(1+i))) + + return decimal \ No newline at end of file diff --git a/CONVERSION SCRIPTS/Number Base Converter/tohex.py b/CONVERSION SCRIPTS/Number Base Converter/tohex.py new file mode 100644 index 00000000..ddcf429a --- /dev/null +++ b/CONVERSION SCRIPTS/Number Base Converter/tohex.py @@ -0,0 +1,128 @@ +from tobinary import octalToBinary + +def decimalToHex(decimal): + binaryNo = '' + + hex_nos = { + '10': 'a', + '11': 'b', + '12': 'c', + '13': 'd', + '14': 'e', + '15': 'f', + } + + decimal = str(decimal) + if '.' in decimal: integral, fractional = decimal.split('.') + else: + integral = decimal + fractional = 0 + + binaryNo = '' + if integral: + integral = int(integral) + while integral > 0: + rem = integral % 16 + if rem >=10: + rem = hex_nos[str(rem)] + binaryNo += str(rem) + integral = integral // 16 + + binaryNo = binaryNo[::-1] + + if fractional: + fractional = '0.' + fractional + binaryNo += '.' + for i in range(20): + prod = float(fractional) * 16 + num = int(prod) + fractional = prod - num + if num >=10: + num = hex_nos[str(num)] + binaryNo += str(num) + + return binaryNo + +def binaryToHex(binary): + binToHex= { + '0000': '0', + '0001': '1', + '0010': '2', + '0011': '3', + '0100': '4', + '0101': '5', + '0110': '6', + '0111': '7', + '1000': '8', + '1001': '9', + '1010': 'a', + '1011': 'b', + '1100': 'c', + '1101': 'd', + '1110': 'e', + '1111': 'f', + '.': '.' + } + + binary = str(binary) + if '.' in binary: integral, fractional = binary.split('.') + else: + integral = binary + fractional = 0 + + if integral: + if len(integral) % 4 == 1: + integral = '000' + integral + if len(integral) % 4 == 2: + integral = '00' + integral + if len(integral) % 4 == 3: + integral = '0' + integral + + groups = [] + bno = '' + for i in range(len(integral)+1): + if len(bno) < 4: + bno += integral[i] + else: + groups.append(bno) + try: bno = integral[i] + except IndexError: pass + + hex = '' + for g in groups: + try: + hex += binToHex[g] + except (KeyError, ValueError): + return 'Invalid Input' + + if fractional: + + if len(fractional) % 4 == 1: + fractional += '000' + if len(fractional) % 4 == 2: + fractional += '00' + if len(fractional) % 4 == 3: + fractional += '0' + + groups = [] + bno = '' + for i in range(len(fractional)+1): + if len(bno) < 4: + bno += fractional[i] + else: + groups.append(bno) + try: bno = fractional[i] + except IndexError: pass + + hex += '.' + for g in groups: + try: + hex += binToHex[g] + except (KeyError, ValueError): + return 'Invalid Input' + + return hex + +def octalToHex(octal): + binary = octalToBinary(octal) + return binaryToHex(binary) \ No newline at end of file diff --git a/CONVERSION SCRIPTS/Number Base Converter/tooctal.py b/CONVERSION SCRIPTS/Number Base Converter/tooctal.py new file mode 100644 index 00000000..84ef693a --- /dev/null +++ b/CONVERSION SCRIPTS/Number Base Converter/tooctal.py @@ -0,0 +1,100 @@ +from tobinary import hexToBinary + +def decimalToOctal(decimal): + decimal = str(decimal) + if '.' in decimal: integral, fractional = decimal.split('.') + else: + integral = decimal + fractional = 0 + + binaryNo = '' + if integral: + integral = int(integral) + while integral > 0: + binaryNo += str(integral % 8) + integral = integral // 8 + + binaryNo = binaryNo[::-1] + + if fractional: + fractional = '0.' + fractional + binaryNo += '.' + for i in range(20): + prod = float(fractional) * 8 + num = int(prod) + fractional = prod - num + binaryNo += str(num) + + return binaryNo + +def binaryToOctal(binary): + binToOct = { + '000': '0', + '001': '1', + '010': '2', + '011': '3', + '100': '4', + '101': '5', + '110': '6', + '111': '7', + } + + binary = str(binary) + if '.' in binary: integral, fractional = binary.split('.') + else: + integral = binary + fractional = 0 + + if integral: + if len(integral) % 3 == 1: + integral = '00' + integral + if len(integral) % 3 == 2: + integral = '0' + integral + + groups = [] + bno = '' + for i in range(len(integral)+1): + if len(bno) < 3: + bno += integral[i] + else: + groups.append(bno) + try: bno = integral[i] + except IndexError: pass + + octal = '' + for g in groups: + try: + octal += binToOct[g] + except (KeyError, ValueError): + return 'Invalid Input' + + if fractional: + + if len(fractional) % 3 == 1: + fractional += '00' + if len(fractional) % 3 == 2: + fractional += '0' + + groups = [] + bno = '' + for i in range(len(fractional)+1): + if len(bno) < 3: + bno += fractional[i] + else: + groups.append(bno) + try: bno = fractional[i] + except IndexError: pass + + octal += '.' + for g in groups: + try: + octal += binToOct[g] + except (KeyError, ValueError): + return 'Invalid Input' + + return octal + +def hexToOctal(hex): + binary = hexToBinary(hex) + return binaryToOctal(binary) + diff --git a/README.md b/README.md index fd830a11..7e89af42 100644 --- a/README.md +++ b/README.md @@ -112,4 +112,5 @@ The contribution guidelines are as per the guide [HERE](https://github.com/larym | 63 | [QtQuiz](https://github.com/larymak/Python-project-Scripts/tree/main/GUI/QtQuiz) | [Eduardo C.](https://github.com/ehcelino) | | 64 | [Umbrella Reminder](https://github.com/larymak/Python-project-Scripts/tree/main/AUTOMATION/Umbrella%20Reminder) | [Edula Vinay Kumar Reddy](https://github.com/vinayedula) | | 65 | [Image to PDF](https://github.com/larymak/Python-project-Scripts/tree/main/IMAGES%20%26%20PHOTO%20SCRIPTS/Image%20to%20PDF) | [Vedant Chainani](https://github.com/Envoy-VC) | -| 66 | [KeyLogger](https://github.com/larymak/Python-project-Scripts/tree/main/OTHERS/KeyLogger) | [Akhil](https://github.com/akhil-chagarlamudi) | \ No newline at end of file +| 66 | [KeyLogger](https://github.com/larymak/Python-project-Scripts/tree/main/OTHERS/KeyLogger) | [Akhil](https://github.com/akhil-chagarlamudi) | +| 67 | [Number Base Converter](https://github.com/larymak/Python-project-Scripts/tree/main/CONVERSION%20SCRIPTS/Number%20Base%20Converter) | [Kunal Dhanawade](https://github.com/kunaldhanawade) |