1

I would like to convert the below hex sequences to images, in the process of sifting through quite a number of problems that are similar to mine none have come close as to that solved in https://stackoverflow.com/a/33989302/13648455, my code is below, where could I be going wrong?

data = "2a2b2c2a2b2c2a2b2c2a2b2cb1"
buf = io.StringIO()    
for line in data.splitlines():
    line = line.strip().replace(" ", "")
    if not line:
        continue
    bytez = binascii.unhexlify(line)
    buf.write(bytez)

with open("image.jpg", "wb") as f:
    f.write(buf.getvalue()) 
Cat-M.bit
  • 17
  • 1
  • 4

1 Answers1

3

io.StringIO() creates a string object which yields a text stream. You need io.BytesIO() instead, which creates a bytes object to which you can write your binary data:

buf = io.BytesIO()

...

buf.write(bytez)

See also io — Core tools for working with streams

Ronald
  • 2,930
  • 2
  • 7
  • 18