Display a string of 1's and 0's in binary format Python
Is there any way to actually get a string of 1's and 0's from a binary format ?
# read in image data fh = open('test.png','rb') data = fh.read() fh.close() # write binary to text file fh = open('test.txt','w') fh.write(data) fh.close fh.close()
how can i turn that data string into 1's and 0's
This will convert each byte in your string to 8 binary digits and separate them with a space. You can easily change the separator.
data = " ".join(bin(ord(b))[2:].rjust(8, "0") for b in data)