0

I am trying to write/ read the string 1's 0's to actual binary file but I have some trouble when I read it. The bytearray when I write is bytearray(b'\xcc\xcc\x9c\xe79\xadkZ\xd6\x0b') but when I read each of byte, it shows weird. The output something like this

b'\x9c'
b'\xe7'
b'9'
b'\xad'
b'k'
b'Z'
....

this is my code

def writefile():
    longstring = "1100110011001100100111001110011100111001101011010110101101011010110101101011"

    with open('file', 'wb') as f:
        bit_strings = [longstring[i:i + 8] for i in range(0, len(longstring), 8)]
        byte_list = [int(b, 2) for b in bit_strings]
        print(bytearray(byte_list))
        f.write(bytearray(byte_list))
        f.close()

def readfile():
    longbin = ""
    with open('file', 'rb') as f:
        #print(f.readline())
        byte = f.read(1)
        while byte != b"":
            print(byte)
            #longbin = longbin + '{0:08b}'.format(ord(byte))
            byte = f.read(1)
    print(longbin)
fastmen111
  • 69
  • 2
  • 9

0 Answers0