Skip to content

Instantly share code, notes, and snippets.

@gceylan
Created July 31, 2011 22:53
Show Gist options
  • Save gceylan/1117313 to your computer and use it in GitHub Desktop.
Save gceylan/1117313 to your computer and use it in GitHub Desktop.
text - binary converter
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Tkinter import *
import sys
def temizle():
kutu1.delete(1.0, END)
kutu2.delete(1.0, END)
def yazdir(liste):
dizgi = ""
for i in liste:
dizgi += i
kutu2.insert(END, dizgi)
def text_to_bin():
metin = kutu1.get(1.0, END)
ascii = ""
ascii_liste = []
for i in metin:
n = ord(i)
while n:
k = n % 2
ascii += str(k)
n /= 2
L = len(ascii)
if L != 8:
ascii = ("0" * (8 - L)) + ascii[::-1]
ascii_liste.append(ascii)
ascii = ""
yazdir(ascii_liste)
def bin_to_text():
bin_sayi = kutu2.get(1.0, END)
text = ''
t = 0
L = len(bin_sayi)
for i in range(0, L, 8):
sayi = bin_sayi[i: i + 8]
sayi = sayi[::-1]
for j in range(0, len(sayi)):
t += (2 ** j) * int(sayi[j])
text += chr(t)
t = 0
kutu1.insert(END, text)
pencere = Tk()
pencere.title("text to bin vs bin to text")
pencere.geometry("620x400+150+100")
L1 = Label(text="Text")
L1.place(relx=0.02, rely=0.05)
kutu1 = Text(height=10, width=40, bg="white", fg="blue",
font="Arial 10")
kutu1.place(relx=0.02, rely=0.1)
L1 = Label(text="Binary")
L1.place(relx=0.52, rely=0.05)
kutu2 = Text(height=10, width=40, bg="white", fg="blue",
font="Arial 10")
kutu2.place(relx=0.52, rely=0.1)
B1 = Button(text="binary çevir", command=text_to_bin)
B1.place(relx=0.02, rely=0.55)
B2 = Button(text="text çevir", command=bin_to_text)
B2.place(relx=0.52, rely=0.55)
B3 = Button(text="temizle", command=temizle)
B3.place(relx=0.52, rely=0.65)
B4 = Button(text="çıkış", command=sys.exit)
B4.place(relx=0.72, rely=0.65)
mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment