使用带有 1 字节变量的 Python struct.unpack
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2051070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Python struct.unpack with 1-byte variables
提问by dpq
How can I use struct.unpack()
or some other function available in Python to easily convert onebyte variable to a Python integer? Right now, it is done in a rather lame way:
我如何使用struct.unpack()
Python 或其他一些可用的函数来轻松地将一个字节变量转换为 Python 整数?现在,它以一种相当蹩脚的方式完成:
file = open("telemetry.dat", "rb").read()
magic = file[0]
int(binascii.hexlify(magic), 16)
Is there another?
还有别的吗?
回答by jsbueno
how about ord(my_byte)
?
怎么样ord(my_byte)
?
Or if the variable content is like my_byte == "0xff"
or ff
you can simply do int(my_byte, 16)
或者如果变量内容是这样的,my_byte == "0xff"
或者ff
你可以简单地做int(my_byte, 16)
If you have a streamof hex digits, you can do:
如果你有一个十六进制数字流,你可以这样做:
int_bytes = (int(my_bytes[i:i+2], 16) for i in xrange(0, len(my_bytes), 2) )
回答by Blaker
Yes, you can use struct.unpack()with 1-Byte variables; see the example below:
是的,您可以将struct.unpack()与 1 字节变量一起使用;请参阅下面的示例:
import struct
my_byte = b'\x07';
my_int = struct.unpack('>H', b'\x00' + my_byte)[0]
print type(my_int)
print my_int
The above example assumes your int is an unsigned int. Take a look at the Format Characters section of the documentationif you need something different (e.g. a signed int, which would be '>h' for the 'fmt' parameter of the unpack function call).
上面的例子假设你的 int 是一个 unsigned int。如果您需要不同的东西,请查看文档的“格式字符”部分(例如,一个带符号的 int,对于解包函数调用的“fmt”参数将是“>h”)。
回答by jfs
An efficient way to interpret each byte from a file as an integer is to use array
module:
将文件中的每个字节解释为整数的一种有效方法是使用array
模块:
import os
from array import array
a = array("B") # interpret each byte as unsigned integer [0, 255]
with open("telemetry.dat", "rb") as file:
a.fromfile(file, os.path.getsize(file.name))
If you already have data as a bytestring; you could use bytearray
or memoryview
(the latter behaves differently on different Python versions):
如果您已经将数据作为字节串;你可以使用bytearray
or memoryview
(后者在不同的 Python 版本上表现不同):
data = b"\xff\x64d"
# a.fromstring(data)
b = bytearray(data)
print(b[0]) # -> 255
Here's the corresponding struct.unpack()
analog (more generic) that returns a tuple:
这struct.unpack()
是返回元组的相应模拟(更通用):
import struct
data = b"\xff\x64d"
t = struct.unpack(len(data)*"B", data)
print(t[-1]) # -> 100
For a single byte represented as a bytestring, you could use ord()
suggested by @jsbueno:
对于表示为字节ord()
串的单个字节,您可以使用@jsbueno 建议:
i = ord(b"d") # -> 100
回答by S.Lott
Are you asking about struct.unpack( 'b', someByte )
?
你问的是struct.unpack( 'b', someByte )
?
A "byte" is a 1-character string.
“字节”是 1 个字符的字符串。
The value 0xff
is an integer, and already unpacked.
该值0xff
是一个整数,并且已经解包。
If you're finding the 4-character value 0xff
in an input file, you're best served by eval()
since your input file contains Python code, 0xff
.
如果您0xff
在输入文件中找到 4 个字符的值,则最好使用它,eval()
因为您的输入文件包含 Python 代码、0xff
.