将 Python 字节转换为“无符号 8 位整数" [英] Convert Python byte to "unsigned 8 bit integer"
本文介绍了将 Python 字节转换为“无符号 8 位整数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从套接字读取字节数组/列表.我希望 Python 将第一个字节视为无符号 8 位整数".怎么可能得到它的整数值作为一个无符号的 8 位整数?
I am reading in a byte array/list from socket. I want Python to treat the first byte as an "unsigned 8 bit integer". How is it possible to get its integer value as an unsigned 8 bit integer?
推荐答案
使用 struct 模块.
import struct
value = struct.unpack('B', data[0])[0]
请注意,解包总是返回一个元组,即使您只解包一个项目.
Note that unpack always returns a tuple, even if you're only unpacking one item.
另外,看看这个问题.
这篇关于将 Python 字节转换为“无符号 8 位整数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文