如何将字节字符串转换为整数值?

我正在从事一个项目,在该项目中,我将 base64 编码图像从我的应用程序发送到进行处理的服务器。服务器收到的图片是这样的:(这个数据量很大)

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

所以,现在我想把它转换成这种格式:[255, 234, 70, 115, ....]。


千巷猫影
浏览 155回答 2
2回答

米脂

只需将列表构造函数扔给它即可。>>> list(b'\xff\xd8\xff\xe1') [255, 216, 255, 225]

心有法竹

假设您使用 Python3,遍历字节字符串实际上会为您提供作为 int 类型的单个值:>>> s = b'\xff\xd8\xff\xe1\x02'>>> for c in s:...&nbsp; &nbsp; &nbsp;print(c, type(c))...&nbsp;255 <class 'int'>216 <class 'int'>255 <class 'int'>225 <class 'int'>2 <class 'int'>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python