1

I'm new to Python Programming and I made a simple Python program about converting text to binary numbers, now i'm adding a simple code for converting them back from binary to text (correct the code below if there is wrong).

message = (input("Enter Text/Binary to Translate: ")

binList = {
   '01100001': 'a', 
   '01100010': 'b', 
   # to Z 
} 

BinT = "".join([binList[message] for message in letter])

print(BinT)

2 Answers 2

3

You should not use a lookup dict for this. To convert a binary string to an ascii character, use built-in functions chr and int respectively:

>>> chr(int('01100001', 2))
'a'

To go the other way, use built-in functions format and ord respectively:

>>> format(ord('a'), '08b')
'01100001'
Sign up to request clarification or add additional context in comments.

1 Comment

What I mean is that if I put a binary value in the program, then it'll convert to as a Text. Do you know other ways of fixing this?
0
message = raw_input("Enter Text/Binary to Translate: ").split(' ')

binList = {
    '01100001': 'a',
    '01100010': 'b',
    # to Z
}

BinT = "".join( [binList[letter] for letter in message])

print(BinT)

1 Comment

You should elaborate, not just fix his code. Explain what is going wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.