1

I am working on a project in which i send a base64 encoded image from my app to a server where the processing happens. The image received on the server is like this: (this data is huge)

b'\xff\xd8\xff\xe1\x02;Exif\x00\x00MM\x00*\x00\.....' 

So, now i want to convert it in this format: [255, 234, 70, 115, ....].

3
  • 3
    Does this answer your question? How to convert a string of bytes into an int? Commented Jul 4, 2020 at 7:46
  • Did you try list(img_data)? Depending on your definition of "huge" (i.e. if it actually doesn't fit in memory) you may want to do for c in iter(img_data) instead. Commented Jul 4, 2020 at 7:53
  • @JordanDimov Yes, didn't know it would be that simple :), Thanks Commented Jul 4, 2020 at 8:10

2 Answers 2

1

Just throw the list constructor at it.

>>> list(b'\xff\xd8\xff\xe1')
[255, 216, 255, 225]
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you use Python3, iterating over the byte string actually gives you individual values as an int type:

>>> s = b'\xff\xd8\xff\xe1\x02'
>>> for c in s:
...     print(c, type(c))
... 
255 <class 'int'>
216 <class 'int'>
255 <class 'int'>
225 <class 'int'>
2 <class 'int'>

Comments