字符串上的 Python/PySerial 位运算符
我正在使用 PySerial (Python 2.7) 从这样的设备读取信息:
buffer += ser.read(3)
现在我在缓冲区中有三个字节(即 0xAE0259),其类型为 str。因为我是Python新手,所以我正在寻找“pythonian”方法来切断三个字节中最左边的(0xAE),然后将剩余的两个解释为int。 首先我想到了一个位掩码:buffer &= 0xFFFF 但 python 不允许我在 str 上使用位运算符。 任何将 buffer 转换为 int 的尝试也失败了。 然后我读到了有关“位串模块”的内容,它让我可以从 BitArray 中切出位范围,但我想使用它来实现此目的有点过头了?
I'm using PySerial (Python 2.7) to read information from a device like this:
buffer += ser.read(3)
Now I have three bytes in buffer (i.e. 0xAE0259) which is of type str. Since I'm new to Python, I'm looking for the "pythonian" way to cut off the left most (0xAE) of the three bytes and then interpret the remaining two as int.
First I thought of a bit mask: buffer &= 0xFFFF
but python won't let me use bit operators on str.
Any attempt to convert buffer to int failed as well.
Then I read about the 'bitstring module' which let's me slice ranges of bits out of a BitArray, but I guess that using it for this would be a little over the top?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要知道多字节类型是大端还是小端,以及它是有符号还是无符号。假设这两个字节是无符号的大端短字节,我会执行以下操作:
“>”表示大端。 “B”是第一个无符号字节,这是您不需要的。 'H' 是一个无符号短整型。
You need to know whether multi-byte types are big or little endian and whether it's signed or unsigned. Assuming the two bytes are an unsigned, big-endian short, I would do the following:
'>' means big endian. 'B' is the first unsigned byte, which you don't want. 'H' is an unsigned short.
有两种简单的方法可以做到这一点。一种方法是将缓冲区转换为十六进制整数,并使用位掩码获取最后 32 位。另一种是使用切片运算符获取最后 4 个字符,并将余数解释为十六进制整数。
编辑 - eryksun 有正确的答案,但我想针对实际用例更新我的示例。
There are two easy ways to do it. One way would be to convert your buffer to a hexdecimal integer, and use a bit-mask to get the last 32 bits. The other is to use the slice operator to get the last 4 characters, and interpret that remainder as a hexadecimal integer.
EDIT - eryksun has the right answer, but I wanted to update my example for the actual use case.
如果您只需要进行字节解包,那么
struct
模块就是您的朋友(请参阅 eryksun 的答案),就像bytearray
类型:这允许您在字节级别进行索引和切片
就将多个字节转换为整数而言,这非常有帮助,但是您'不太可能获得太多收益
struct
除非你有一些不寻常的字节长度。您的两个字节转换为 int 变为:您在评论中说您也想要一种进行按位切片的通用方法。恐怕没有 - 你最好的起点是从字节数组中进行移位和屏蔽。这就是为什么像 bitstring 这样的模块很有用(顺便说一句,我写了它) - 你让其他人来完成所有繁琐的工作容易出错的东西!
If you only need to do byte unpacking then the
struct
module is your friend (see eryksun's answer) as is thebytearray
type:This allows you to index and slice at the byte level
In terms of converting multiple bytes to ints this is quite helpful, but you're unlikely to gain much over
struct
unless you have some unusual byte lengths. Your two byte conversion to int becomes:You say in a comment that you'd like a general way of doing bitwise slicing too. I'm afraid there isn't one - your best place to start is with shifting and masking from a bytearray. That's why modules like bitstring are useful (I wrote it btw) - you get someone else to do all the tedious error-prone stuff!
如果
buffer
是一个字符串,您可以通过修剪字符串来“切断”剩余的字符,如下所示:if
buffer
is a string, you could "cut off" the remaining characters by trimming the string like this:@ironchefpython 建议您应该使用什么,除了您的注释表明您的缓冲区实际上由二进制数据组成。假设是这种情况,这个解决方案应该可以工作,尽管它不是很优雅:
这会产生您想要的结果 - 例如:
@ironchefpython suggested what you should use, except that your comment indicates that your buffer actually consists of binary data. Assuming that is the case, this solution should work, though it is not very elegant:
This yields the result you want -for example: