Welcome folks today in this post we will be building text to binary code
gui desktop app using regular expression library in tkinter. All the full source code of the application is given below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#!/usr/bin/env python __author__ = 'Max DeLiso' import tkinter import tkinter.constants from binutils import binEncodeString, attemptDecode, binDecodeString class TkTest(tkinter.Frame): def __init__(self, master=None): tkinter.Frame.__init__(self, master) #TODO: refactor these into a class for brevity self.INPUT_L = tkinter.Text(self) self.INPUT_L.grid(column=0, row=1, sticky=tkinter.NSEW, padx=2, pady=2) self.INPUT_L.bind("<KeyRelease>", self.on_left_text_update) self.INPUT_L.bind("<FocusIn>", self.on_left_text_update) self.INPUT_R = tkinter.Text(self) self.INPUT_R.grid(column=1, row=1, sticky=tkinter.NSEW, padx=2, pady=2) self.INPUT_R.bind("<KeyRelease>", self.on_right_text_update) self.INPUT_R.bind("<FocusIn>", self.on_right_text_update) self.LABEL_TOP = tkinter.Label( self, text="text in left field, binary strings in the right", justify=tkinter.constants.CENTER) self.LABEL_TOP.grid(row=0, columnspan=2, sticky=tkinter.S, pady=4) self.grid_columnconfigure(0, weight=1) self.grid_columnconfigure(1, weight=1) self.grid_rowconfigure(0, minsize=16) self.grid_rowconfigure(1, weight=1) def on_left_text_update(self, ev): self.INPUT_R.replace("0.0", tkinter.END, binEncodeString(self.INPUT_L.get("0.0", tkinter.END))) def on_right_text_update(self, ev): # TODO: do this line by line with insert/delete self.INPUT_L.replace("0.0", tkinter.END, binDecodeString(self.INPUT_R.get("0.0", tkinter.END))) if __name__ == "__main__": root = tkinter.Tk() app = TkTest(root) app.grid(sticky=tkinter.NSEW) root.title("bin2text") root.grid_columnconfigure(0, weight=1) root.grid_rowconfigure(0, weight=1) root.resizable(True, True) root.mainloop() |
Now make a binutils.py
file and copy paste the following code
binutils.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import re def binEncodeString(rawString): return ' '.join( map(lambda ch: str(bin(ord(ch))[2:]), rawString)) def attemptDecode(numStr): try: return str(chr(int(numStr, base=2))) except ValueError: pass except UnicodeEncodeError: pass def binDecodeString(binStr): return ''.join( filter(None, list(map(attemptDecode, re.findall(r'[0-1]+', binStr))))) |
Now run the python script
by typing the below command as shown below
python app.py