0

I have written a code where I can convert the text from a text file a.txt into a binary string Binary, now I want do the reverse,

In other words, I want to convert binary string Binary into a text file b.txt which has same text as the text file a.txt,

how can I do that?

The code is given below, plz provide solution according to the code:

content = open('a.txt', 'r').read()
test_str = content
# using join() + ord() + format()  ... Converting String to binary 
Binary = ''.join(format(ord(i), 'b') for i in test_str)   

# printing original string  
print("The original string is : " + str(test_str)) 
# printing result  
print("The string after Binary conversion : \n" + str(Binary))

Edit:

  1. I tried to convert the binary string Binary into a text string but I got unknown character, which is not in the text file a.txt.

  2. If I provide space in ''.join(format(ord(i), 'b') for i in test_str) then I can not get a sentence in the string also, I get error, the string gets spaces, which i don't need, I need a complete string without space in the string Binary. I tried below code for reconverting :

    n = int(Binary, 2)

    print(n.to_bytes((n.bit_length() + 7) // 8,'big').decode())

Disclaimer related to Duplication Post:

This is not a duplicate the post you are referring is different, the post asks about "in other words, if i have binary number, i want to convert it to a text file." which is completely different,

14
  • 1
    This is not possible because there is no way to tell where one binary number ends and the next starts in the output. Commented Mar 15, 2020 at 1:24
  • 3
    What is the issue, exactly? Have you tried anything, done any research? Stack Overflow is not a free code writing service. See: How to Ask, help center, meta.stackoverflow.com/questions/261592/…. Commented Mar 15, 2020 at 1:25
  • @AMC Plz check the edit of the post. Commented Mar 15, 2020 at 1:32
  • Instead of ''.join(...) consider joining with a space: ' '.join(...) Then you will be able split back to characters because the output will be like: 11111100 110001 instead of a long string of ones and zeros. Commented Mar 15, 2020 at 1:32
  • @MichaelButscher so should I need to get a different way to get string Binary so it can be later converted easily to a text string? Commented Mar 15, 2020 at 1:33

1 Answer 1

1

You need some way of identifying character borders. If you limit this to a set bit length — like only 8-bit, you can pad the binary and then you'll know the character size. If you don't want to do this you need some other way.

Here is a method that doesn't care about the input — it handles spaces, emojis, etc. It does this by separating the characters in the binary with a space:

test_str = "Dies ist eine binäre Übersetzung. 🐻"

Binary = ' '.join(format(ord(i), 'b') for i in test_str)   

print("original:")
print(test_str)

print("\nThe string after Binary conversion : \n" + Binary)

text = "".join(chr(int(s, 2)) for s in  Binary.split())
print(f'\nString after conversion back to text:\n{text}')

This prints:

original:
Dies ist eine binäre Übersetzung. 🐻

The string after Binary conversion :
1000100 1101001 1100101 1110011 100000 1101001 1110011 1110100 100000 1100101 1101001 1101110 1100101 100000 1100010 1101001 1101110 11100100 1110010 1100101 100000 11011100 1100010 1100101 1110010 1110011 1100101 1110100 1111010 1110101 1101110 1100111 101110 100000 11111010000111011

String after conversion back to text:
Dies ist eine binäre Übersetzung. 🐻

Notice the last character for the emoji and how long the binary is. That could be a bear emoji or a couple ascii characters. Without a separator, there's now way to know.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Mark!, I am trying to get binary string from this code: ` with open("a.txt", "rb") as file: binary_data = file.read() ` then used Binary = ' '.join(format(ord(i), 'b') for i in binary_data ) but it is not working, I don't get binary string as above, can u help me? where I need to change?
@MikeSQ when you save a string like "1101" to a file it's a string, not binary data. So opening with "rb" is not really the right way to go.
Is there a way to see variable binary_data in binary format? Name the function plz if it exists, I will google it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.