1

I'm trying to decode binary which are located in a .txt file, but I'm stuck. I don't see any possibilities this can go around.

def code(): testestest
  ascii = {'01000001':'A', ...}
  binary = {'A':'01000001', ...}
  print (ascii, binary)

  def encode():
    pass

  def decode(code,n):
    f = open(code, mode='rb') # Open a file with filename <code>
    while True:
      chunk = f.read(n)           # Read n characters at time from an open file
      if chunk == '':             # This is one way to check for the End Of File in Python 
        break
      if chunk != '\n':
        # Process it????
        pass

How can I take the binary in the .txt file and output it as ASCII?

Musa
  • 65
  • 1
  • 8
  • Oh, yeah :-) I'm sorry. Is there any easy way the binary from the .txt file be outputted to ASCII? – Musa Jan 28 '16 at 14:11
  • What's your input? What's the expected output? This sound more easy than you're making it look. – tglaria Jan 28 '16 at 14:13
  • 1
    Your answer is [**here**](http://stackoverflow.com/a/7397689/5756174). Just modify it according to your need. – jblixr Jan 28 '16 at 14:15
  • Is it duplicate of http://stackoverflow.com/q/7396849/1566267: `n = int('0b110100001100101011011000110110001101111', 2)` – John_West Jan 28 '16 at 14:15
  • 2
    can't you use `int("01000001', 2)` to convert into integer and then `chr(65)` to get char ? – furas Jan 28 '16 at 14:16
  • 1
    You can convert between binary, ascii, and strings using [**`bin`**](https://docs.python.org/2/library/functions.html#bin), [**`int`**](https://docs.python.org/2/library/functions.html#int), and [**`chr`**](https://docs.python.org/2/library/functions.html#chr). – Peter Wood Jan 28 '16 at 14:16
  • It makes it more complicated when it should convert the binary from a .txt, instead of clear binary in the code. I'm sorry, but I'm a total noob in python, but I love the language and would love to know this as a base for further programming. – Musa Jan 28 '16 at 14:22
  • Your comments don't make much sense. *What* makes *what* more complicated and *how*? Honestly, the link that both jblixr and John_West gave you solve your problem. You just have to read in the whole binary string in one go and feed it into that function. What exactly are you struggling with in regards to that? – Reti43 Jan 28 '16 at 14:45

1 Answers1

6

From your example, your input looks like a string of a binary formatted number.

If so, you don't need a dictionnary for that:

def byte_to_char(input):
     return chr(int(input, base=2))

Using the data you gave in the comments, you have to split your binary string into bytes.

input ='01010100011010000110100101110011001000000110100101110011001000000110101001110101011100110111010000100000011000010010000001110100011001010111001101110100001000000011000100110000001110100011000100110000'
length = 8
input_l = [input[i:i+length] for i in range(0,len(input),length)]

And then, per byte, you convert it into a char:

input_c = [chr(int(c,base=2)) for c in input_l]
print ''.join(input_c)

Putting it all together:

def string_decode(input, length=8):
    input_l = [input[i:i+length] for i in range(0,len(input),length)]
    return ''.join([chr(int(c,base=2)) for c in input_l])

decode(input)
>'This is just a test 10:10'
tglaria
  • 5,678
  • 2
  • 13
  • 17
  • Thanks, but I think there need to be a dictionary for holding the mapping between binary string and ascii chars? This is the binary: 01010100011010000110100101110011001000000110100101110011001000000110101001110101011100110111010000100000011000010010000001110100011001010111001101110100001000000011000100110000001110100011000100110000 – Musa Jan 28 '16 at 14:23
  • Is that a question? No, you don't need it. you convert your binary string into an integer and then convert that integer into ascii. – tglaria Jan 28 '16 at 14:24
  • Thanks, but the binary string is in an external file in a folder as mentioned. – Musa Jan 28 '16 at 14:32
  • So? just load that string. – tglaria Jan 28 '16 at 14:37
  • So in it will be... def code(): bin = int('0b110100001100101011011000110110001101111', 2) pass – Musa Jan 28 '16 at 14:40
  • and... def decode(input): return chr(int(input, base=2)) pass – Musa Jan 28 '16 at 14:40
  • that don't work for me..do you have a quick solution for this please? I'm a total noobie:) – Musa Jan 28 '16 at 14:41
  • No, you've got to split your data into chars, each chunk of 8 bits is one byte, each byte is a char. – tglaria Jan 28 '16 at 14:41
  • Yeah, that makes me confused. Thanks – Musa Jan 28 '16 at 14:43
  • Yeah, I was just translating one byte into a char. But I guess now it's all clear? – tglaria Jan 28 '16 at 14:45
  • Thanks for your time and the code. But again, this doesnt get the binary number from an external .txt file, which it should do. Much appreciated @tglaria :) – Musa Jan 28 '16 at 15:04
  • I can't get that part for you, I don't know the format of your file. Any answer would be guessing/assuming something, and I'd rather not. your shouldn't have any problem once you've loaded the file and get the info as a string. (I would assume that if the data is text, then you don't need to do any conversion). – tglaria Jan 28 '16 at 15:13
  • The line `print '.join(input_c)'` is wrong. It should be `print ''.join(input_c)`. – Reti43 Jan 28 '16 at 15:32