Python Forum
Simple Binary Translator (Using MATH instead of MATCH))
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Binary Translator (Using MATH instead of MATCH))
#1
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
Reply
#2
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'
Reply
#3
(Jun-09-2020, 02:17 AM)Larz60+ Wrote: 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'

I admit that I did not know that, but on a positive note, I made it all without using any imports.
Reply
#4
imports are your friends
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The LOL Translator SpeedyZapGaming 7 6,190 Apr-15-2017, 08:54 AM
Last Post: GamingAgent

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020