Search code examples
pythonpython-3.xwindowsbinaryfiles

Read a file and write the file's binary to a text document


I am using Python 3.10 and I am trying to read a Windows executable file, and output the binary data in a text format (1's and 0's) into a text file. I only require the binary, I do not want the offsets and the ASCII representation of the bytes. I do not want any spaces or new lines.

total = ""
with open("input.exe", "rb") as f:
    while (byte := f.read(1)):
        total = total + byte.decode("utf-8")
with open("output.txt", "w") as o:
    o.write(total)

However, it is not working, I am presented with the following error.

Traceback (most recent call last):
    File "converter.py", line 4, in <module>
        total = total + byte.decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x90 in position 0: invalid start byte

Solution

  • This code should do what you want:

    with open("input.exe", "rb") as f:
        buf = f.read()
    
    with open("output.txt", "w") as o:
        binary = bin(int.from_bytes(buf, byteorder='big'))[2:] # or byteorder='little' as necessary
        o.write(binary)
    

    Beware that it might hog up memory when dealing with very large files.