Simple Binary Translator (Using MATH instead of MATCH)) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: Simple Binary Translator (Using MATH instead of MATCH)) (/thread-27499.html) |
Simple Binary Translator (Using MATH instead of MATCH)) - DataCorrupt - Jun-09-2020 I decided to write a text to binary translator only using math. This was to test my abilities because I am very new. This can only translate letters and nothing else. I knew how to do binary in my head, so I made a program without using a dictionary to simply match. def Binary(phrase): ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" def Find(letter): Count = 0 for LETTER in ALPHABET: Count += 1 if letter.upper() == LETTER: return Count break result = "" for letter in phrase: try: output = "" value = 0 if letter.upper() in ALPHABET: if letter.upper() == letter: value += 1000000 spot = Find(letter) if spot >= 16: value += 10000 spot -= 16 if spot >= 8: value += 1000 spot -= 8 if spot >= 4: value += 100 spot -= 4 if spot >= 2: value += 10 spot -= 2 if spot >= 1: value += 1 spot -= 1 output += str(value) elif letter.lower() == letter: value += 1100000 spot = Find(letter) if spot >= 16: value += 10000 spot -= 16 if spot >= 8: value += 1000 spot -= 8 if spot >= 4: value += 100 spot -= 4 if spot >= 2: value += 10 spot -= 2 if spot >= 1: value += 1 spot -= 1 output += str(value) result += "0" + output + " " else: result += "00100000 " except: pass return result RE: Simple Binary Translator (Using MATH instead of MATCH)) - Larz60+ - Jun-09-2020 to find positionof letter, use: import string alphabet = string.ascii_uppercase def get_char_pos(letter): return alphabet.index(letter.upper()) # testit note 'A' = 0: print(f"Q is at position: {get_char_pos('Q')}")you can also get the value of the character and subtract value of 'A' RE: Simple Binary Translator (Using MATH instead of MATCH)) - DataCorrupt - Jun-09-2020 (Jun-09-2020, 02:17 AM)Larz60+ Wrote: to find positionof letter, use: I admit that I did not know that, but on a positive note, I made it all without using any imports. RE: Simple Binary Translator (Using MATH instead of MATCH)) - Larz60+ - Jun-09-2020 imports are your friends |