简体   繁体   English

带有 TypeError 的 Python struct.unpack 错误:需要一个类似字节的对象,而不是“str”

[英]Python struct.unpack errors with TypeError: a bytes-like object is required, not 'str'

Can someone please help with the following line of code and Error?有人可以帮忙解决以下代码行和错误吗? I am unfamiliar with python value conversions.我不熟悉python值转换。

The specific line that generates the error is:产生错误的具体行是:

value = struct.unpack("<h",chr(b)+chr(a))[0]

TypeError: a bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,而不是“str”

The code fragment is:代码片段是:

                    if packet_code ==0x80: # raw value
                        row_length = yield
                        a = yield
                        b = yield
                        value = struct.unpack("<h",chr(b)+chr(a))[0]

The input data is:输入数据为:

b'\x04\x80\x02\x00\xb2\xcb\xaa\xaa\x04\x80\x02\x00p\r\xaa\xaa\x04\x80\x02\x00] \xaa\xaa\x04\x80\x02\x00@=\xaa\xaa\x04\x80\x02\x007F\xaa\xaa\x04\x80\x02\x00\!\xaa\xaa\x04\x80\x02\x00=@\xaa\xaa\x04\x80\x02\x00=@\xaa\xaa\x04\x80\x02\x00i\x14\xaa\xaa\x04\x80\x02\x00] \xaa\xaa\x04\x80\x02\x00p\r\xaa\xaa\x04\x80\x02\x00\x80\xfd\xaa\xaa b'\x04\x80\x02\x00\xb2\xcb\xaa\xaa\x04\x80\x02\x00p\r\xaa\xaa\x04\x80\x02\x00] \xaa\xaa\x04\x80\ x02\x00@=\xaa\xaa\x04\x80\x02\x007F\xaa\xaa\x04\x80\x02\x00\!\xaa\xaa\x04\x80\x02\x00=@\xaa\xaa\ x04\x80\x02\x00=@\xaa\xaa\x04\x80\x02\x00i\x14\xaa\xaa\x04\x80\x02\x00] \xaa\xaa\x04\x80\x02\x00p\r \xaa\xaa\x04\x80\x02\x00\x80\xfd\xaa\xaa

I am using python 3.5.我正在使用 python 3.5。 This code seems to work in the older versions.此代码似乎适用于旧版本。

Here is the link to similar parser code where it may have worked with previous versions of Python: Parser Code Link这是类似解析器代码的链接,它可能与以前版本的 Python 一起使用: Parser Code Link

Here is the link to the description of how the data is sent from the device RAW Wave Value (16-bit)这是关于如何从设备RAW Wave Value(16 位) 发送数据的说明的链接

This Data Value consists of two bytes, and represents a single raw wave sample.该数据值由两个字节组成,代表一个原始波形样本。 Its value is a signed 16-bit integer that ranges from -32768 to 32767. The first byte of the Value represents the high-order bits of the twos-compliment value, while the second byte represents the low-order bits.它的值是一个有符号的 16 位整数,范围从 -32768 到 32767。Value 的第一个字节表示二进制补码值的高位,而第二个字节表示低位。 To reconstruct the full raw wave value, simply shift the first byte left by 8 bits, and bitwise-or with the second byte:要重建完整的原始波形值,只需将第一个字节左移 8 位,然后将第二个字节按位或按位:

short raw = (Value[0]<<8) | Value[2];

where Value[0] is the high-order byte, and Value 1 is the low-order byte.其中 Value[0] 是高位字节,Value 1是低位字节。

In systems or languages where bit operations are inconvenient, the following arithmetic operations may be substituted instead:在位运算不方便的系统或语言中,可以用以下算术运算代替:

raw = Value[0]*256 + Value[1];
if( raw >= 32768 ) raw = raw - 65536;

Really appreciate any help as I am currently stuck.非常感谢任何帮助,因为我目前陷入困境。

When you are using Python 2.x str is a byte array.当您使用 Python 2.x 时, str是一个字节数组。 For Python 3, you must use bytes like this:对于 Python 3,您必须使用如下bytes

struct.unpack("<h", bytes([b, a]))[0]

if you use python3 you can use the following lines for the received data and convert it to a short data type.如果您使用 python3,您可以使用以下几行接收到的数据并将其转换为短数据类型。

struct.unpack('<h', data)
struct.unpack('<h', data[0:4])
struct.unpack('<h', b''.join(…))

If it receives the data as a list, it uses converts the array to bytes:如果它以列表的形式接收数据,它使用将数组转换为字节:

struct.unpack('<h', bytes(data))

Remember you must convert your information to bytes and not send as str, in order to use unpack and decompress the information in the data type you require.请记住,您必须将您的信息转换为字节而不是作为 str 发送,以便使用所需数据类型的解包和解压缩信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:需要一个类似字节的对象,而不是“str” - TypeError: a bytes-like object is required, not 'str' TypeError:需要类似字节的 object,而不是“str”? - TypeError: a bytes-like object is required, not 'str'? 需要一个类似字节的对象,而不是&#39;str&#39;:TypeError - a bytes-like object is required, not 'str' : TypeError TypeError:需要一个类似字节的对象,而不是&#39;str&#39; - TypeError: a bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,而不是&#39;str&#39;python - TypeError: a bytes-like object is required, not 'str' python TypeError:需要一个类似字节的对象,而不是 Python 中 Image 命令的“str” - TypeError: a bytes-like object is required, not 'str' for Image command in Python Python TypeError:需要一个类似字节的对象,而不是&#39;str&#39; - Python TypeError: a bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,而不是&#39;str&#39;python3 - TypeError: a bytes-like object is required, not 'str' python3 类型错误:需要类似字节的 object,而不是使用子进程 python 的“str” - TypeError: a bytes-like object is required, not 'str' using Subprocess python TypeError:需要一个类似字节的对象,对于无服务器和Python3来说不是&#39;str&#39; - TypeError: a bytes-like object is required, not 'str' with serverless and Python3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM