Cat-M.bit
Cat-M.bit

Reputation: 27

TypeError: string argument expected, got 'bytes'

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()) 

Upvotes: 2

Views: 4564

Answers (1)

Ronald
Ronald

Reputation: 3315

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

Upvotes: 5

Related Questions