Eren Biçer
Eren Biçer

Reputation: 171

Text to Binary in Python

I'm trying to write a binary translator - with a string as an input and its binary representation as output.

And I'm having some difficulties, I wrote in variable the translation for each letter, but they are variables, not strings, so I want to take an input from that matches with the name of the variable and prints the result:

a = "01000001"
b = "01000010"
c = "01000011"
d = "01000100"
# all the way to z


word = input("enter here: ")
print (word)

When I run this I enter a word and it just returns the same word to me but when I write print(a) it returns 01000010 but I can't get it to work with the input.

Can someone tell me where I'm doing something wrong?

Upvotes: 5

Views: 850

Answers (2)

MarianD
MarianD

Reputation: 14201

Probably the more correct solution is to use a dictionary instead of names of letters as names of variables:

transDict = {
    "a": "01100001",
    "b": "01100010",
    "c": "01100011",
    "d": "01100100",
    # etc., etc.
    }

text   = input( "Enter your message: " )

result = "".join( [transDict[letter] for letter in text] )

print(result)

(I also corrected the ASCII codes - yours were for capital letters.)


The explanation
(for the longest statement):

"Use "" as delimiter (i.e. no delimiter) to join all items in the list of translated letters where letters are gotten one after other from the text".

So the result will be the same as if you used these commands:

listOfCodes = []                     # Starting with an empty list
for letter in text:                  # For letter by letter in text perform 2 actions:
    code = transDict[letter]         #   - translate the letter
    listOfCodes.append( code )       #   - append the translation to the list

result = "".join( listOfCodes )      # Then join items of the list without a delimiter 
                                     # (as "" is an empty string) (" " would be nicer)

Upvotes: 3

developer_hatch
developer_hatch

Reputation: 16214

Following the comments of the users, is a better practice of programming using a dictionary for these cases, you just only have to fill in the dictionary letterToBin as you can see in the example

This is a dictionary, wich means it will have a key, and a value, like a cell phone, you have the key as a name (your mother) and the value (his cellphone):

letterToBin = {}

letterToBin = {
  "a" : "01000001", #Here, the key is the "a" letter, and the value, his bin transformation, the 01000001
  "b" : "01000010",
  "c" : "01000011",
  "d" : "01000100"
  #so you need to add all the other keys you need, for example the "e"
  "e" : "01000101" #for example
}




binToLetter = {} # here I create a second dictionary, and it invert the values of the first, it meas, now the keys will be the bins, and the value the latters
binToLetter = dict(zip(letterToBin.values(), letterToBin.keys())) #this code do the magic, you must understand, that only needs to feel the first dictionary, and for free, you will have the second dictionary

wordOrBin = input("enter here: ")

if wordOrBin in letterToBin:
  print(letterToBin[wordOrBin]) #here I has if you write a latter (a) or a bin(11001101) and it choose where to look the correct value
else:
  print(binToLetter[wordOrBin])

Upvotes: 5

Related Questions