Python Forum
Simple text to binary python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple text to binary python script
#1
Hi all,

super new to python and working on a text to binary convertor using a dictionary, and wanting to understand how I get back to text after I have the binary output.

I currently have this;
def to_text_code(text):
    code = {'a':'01100001','b':'01100010','c':'01100011}

    text_code = ""

    for x in text:
        text_code += code[x.lower()]
    return text_code

text = input("Text to Binary Converter: ")
print(to_text_code(text))
It seems to work for the letters that I have placed in my dictionary and I can have multiple entries. However, when reversing the dictionary to go from binary back to text I'm having lots of issues.

Any help would be great,

thanks,

G
Reply
#2
What exactly is your problem? Having difficulties of splitting an array of bits into bytes? Try this:
sub split_bitrange_in_bytes(string_with_bits)
    for bitcounter in range(0, len(string_with_bits), 8):
        print(string_with_bits[bitcounter:bitcounter + 8])
Reply
#3
(Feb-04-2020, 07:00 PM)ibreeden Wrote: However, when reversing the dictionary to go from binary back to text I'm having lots of issues.
You really don't need to make this dictionary(a lot of job for all characters Doh) as Python has tool build in for convert.
When you have the dictionary can look at doing it both ways as a exercise.
def to_text_code(text):
    code = {
        "a": "01100001",
        "b": "01100010",
        "c": "01100011",
    }
    if text.isalpha():
        return code.get(text, f'No record for <{text}>')
    else:
        rev = {v: k for k, v in code.items()}
        return rev.get(text, f'No record for <{text}>')

lst = ['a', '01100001', 'b', "01100010"]
for text in lst:
    print(to_text_code(text)
Output:
01100001 a 01100010 b
Convert back and forth,can try to make function using this.
>>> c = 'a'
>>> c = f"{ord(c):08b}"
>>> c
'01100001'
>>> 
>>> chr(int(c, 2))
'a'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Script that alternates between 2 text messages DiscoMatic 1 515 Dec-12-2023, 03:02 PM
Last Post: buran
  Python Code for Preorder Traversal of a Binary Tree Bolt 1 593 Sep-22-2023, 09:32 AM
Last Post: Gribouillis
  Python script that deletes symbols in plain text nzcan 3 687 Sep-05-2023, 04:03 PM
Last Post: deanhystad
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,181 Jun-29-2023, 11:57 AM
Last Post: gologica
  How do I read and write a binary file in Python? blackears 6 6,497 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Simple Python script, path not defined dubinaone 3 2,692 Nov-06-2021, 07:36 PM
Last Post: snippsat
  How to convert binary data into text? ZYSIA 3 2,622 Jul-16-2021, 04:18 PM
Last Post: deanhystad
  How to assigned value to each different binary in python... ZYSIA 2 2,043 Jul-12-2021, 11:01 AM
Last Post: Gribouillis
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,131 Aug-09-2020, 02:12 PM
Last Post: snippsat
  How to convert a python code to binary? rsurathu 0 1,794 Aug-02-2020, 08:09 AM
Last Post: rsurathu

Forum Jump:

User Panel Messages

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