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
    Does this answer your question? [How to convert a string of bytes into an int?](https://stackoverflow.com/questions/444591/how-to-convert-a-string-of-bytes-into-an-int) – Shivam Jha Jul 04 '20 at 07: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. – Jordan Dimov Jul 04 '20 at 07:53
  • @JordanDimov Yes, didn't know it would be that simple :), Thanks –  Jul 04 '20 at 08:10

2 Answers2

1

Just throw the list constructor at it.

>>> list(b'\xff\xd8\xff\xe1')
[255, 216, 255, 225]
timgeb
  • 76,762
  • 20
  • 123
  • 145
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'>
TheEdgeOfRage
  • 565
  • 1
  • 8
  • 21