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,

Michael
  • 191
  • 3
  • 16
  • 1
    This is not possible because there is no way to tell where one binary number ends and the next starts in the output. – Michael Butscher Mar 15 '20 at 01:24
  • 3
    What is the issue, exactly? Have you tried anything, done any research? Stack Overflow is not a free code writing service. See: [ask], [help/on-topic], https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users. – AMC Mar 15 '20 at 01:25
  • @AMC Plz check the edit of the post. – Michael Mar 15 '20 at 01: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. – Mark Mar 15 '20 at 01: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? – Michael Mar 15 '20 at 01:33
  • @MikeSQ _Plz check the edit of the post._ Yes? – AMC Mar 15 '20 at 01:35
  • This question has been posted twice: https://stackoverflow.com/questions/60687701/python-converting-a-text-file-to-a-binary-file#comment107372928_60687701 –  Mar 15 '20 at 01:35
  • @MikeSQ Mark Meyer commented with a solution. – Michael Butscher Mar 15 '20 at 01:35
  • @MarkMeyer there is a problem! 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())` – Michael Mar 15 '20 at 01:45
  • If you encode with a space between characters, you can decode with something like: `"".join(chr(int(s, 2)) for s in Binary.split())` – Mark Mar 15 '20 at 01:47
  • @MarkMeyer does not work. – Michael Mar 15 '20 at 01:54
  • @MikeSQ I'm not sure how to help "does not work" It's working fine here. The main issue without having some separator can be seen with the simple string; `1z`. With your method you get `1100011111010` which is the same string you get with input of `c:` — there's no way to know which is the right reverse translation. – Mark Mar 15 '20 at 02:02
  • @MarkMeyer there should be a binary value for space, why it is not present in the `Binary`, any idea? – Michael Mar 15 '20 at 02:14
  • @MikeSQ easier just to show in code. See answer below. – Mark Mar 15 '20 at 02:19

1 Answers1

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.

Mark
  • 90,562
  • 7
  • 108
  • 148
  • 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? – Michael Mar 20 '20 at 01:07
  • @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. – Mark Mar 20 '20 at 01:13
  • Is there a way to see variable `binary_data` in binary format? Name the function plz if it exists, I will google it. – Michael Mar 20 '20 at 01:56