|
| 1 | +#Binary Encoder/Decoder |
| 2 | +binary_dict={ |
| 3 | + 'A': '01000001', 'B': '01000010', 'C': '01000011', 'D': '01000100', 'E': '01000101', |
| 4 | + 'F': '01000110', 'G': '01000111', 'H': '01001000', 'I': '01001001', 'J': '01001010', |
| 5 | + 'K': '01001011', 'L': '01001100', 'M': '01001101', 'N': '01001110', 'O': '01001111', |
| 6 | + 'P': '01010000', 'Q': '01010001', 'R': '01010010', 'S': '01010011', 'T': '01010100', |
| 7 | + 'U': '01010101', 'V': '01010110', 'W': '01010111', 'X': '01011000', 'Y': '01011001', |
| 8 | + 'Z': '01011010', 'a': '01100001', 'b': '01100010', 'c': '01100011', 'd': '01100100', 'e': '01100101', |
| 9 | + 'f': '01100110', 'g': '01100111', 'h': '01101000', 'i': '01101001', 'j': '01101010', |
| 10 | + 'k': '01101011', 'l': '01101100', 'm': '01101101', 'n': '01101110', 'o': '01101111', |
| 11 | + 'p': '01110000', 'q': '01110001', 'r': '01110010', 's': '01110011', 't': '01110100', |
| 12 | + 'u': '01110101', 'v': '01110110', 'w': '01110111', 'x': '01111000', 'y': '01111001', |
| 13 | + 'z': '01111010', '0': '00110000', '1': '00110001', '2': '00110010', '3': '00110011', '4': '00110100', |
| 14 | + '5': '00110101', '6': '00110110', '7': '00110111', '8': '00111000', '9': '00111001', |
| 15 | +'.': '00101110', ',': '00101100', '!': '00100001', '?': '00111111', |
| 16 | + ':': '00111010', ';': '00111011', '-': '00101101', '_': '01011111', '(': '00101000', |
| 17 | + ')': '00101001', '[': '01011011', ']': '01011101', '{': '01111011', '}': '01111101', |
| 18 | + '"': '00100010', "'": '00100111', '@': '01000000', '#': '00100011', '$': '00100100', |
| 19 | + '%': '00100101', '^': '01011110', '&': '00100110', '*': '00101010', '+': '00101011', |
| 20 | + '=': '00111101', '/': '00101111', '\\': '01011100', '<': '00111100', '>': '00111110' |
| 21 | +} |
| 22 | + |
| 23 | +reverse_binary_dict = {v:k for k,v in binary_dict.items()} |
| 24 | + |
| 25 | +mode=input("Type 'encode' to convert text to binary or 'decode' to convert binary to text") |
| 26 | + |
| 27 | +if mode=='encode': |
| 28 | + text_input=input("Enter Text:") |
| 29 | + binary_output="" |
| 30 | + for char in text_input: |
| 31 | + if char in binary_dict: |
| 32 | + binary_output += binary_dict[char] |
| 33 | + elif char== " ": |
| 34 | + binary_output += " " |
| 35 | + else: |
| 36 | + binary_output += "?" |
| 37 | + print("Binary: ",binary_output) |
| 38 | +elif mode=='decode': |
| 39 | + binary_input = input("Enter Binary: ") |
| 40 | + text_output = "" |
| 41 | + i=0 |
| 42 | + while i+8 <= len(binary_input): |
| 43 | + binary_chunk=binary_input[i:i+8] |
| 44 | + if binary_chunk in reverse_binary_dict: |
| 45 | + text_output += reverse_binary_dict[binary_chunk] |
| 46 | + else: |
| 47 | + text_output += "?" |
| 48 | + i+=8 |
| 49 | +print("Decoded Text:",text_output) |
0 commit comments