struct.error: bad char in struct format when i want to use struct.pack()
struct.error: bad char in struct format when i want to use struct.pack()
我想打包我的数据以通过套接字发送。
我做到了
sensor = b'cam'
msg = struct.pack('3s >I >I', sensor, len(channel), len(inf_bytes)) + channel + inf_bytes ```
And the I got: struct.error: bad char in struct format
Could you tell me where I am wrong?
只有格式字符串中的第一个字符可以>使用big-endian:
>>> struct.pack('3s>I>I', b'A', 2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
struct.error: bad char in struct format
>>> struct.pack('>3sII', b'A', 2, 3)
b'A\x00\x00\x00\x00\x00\x02\x00\x00\x00\x03'
医生说:(强调我的)
By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler).
Alternatively, the first character of the format string can be used to indicate the byte order, size and alignment of the packed data, according to the following table:
在https://docs.python.org/3/library/struct.html#struct-format-strings
我想打包我的数据以通过套接字发送。
我做到了
sensor = b'cam'
msg = struct.pack('3s >I >I', sensor, len(channel), len(inf_bytes)) + channel + inf_bytes ```
And the I got: struct.error: bad char in struct format
Could you tell me where I am wrong?
只有格式字符串中的第一个字符可以>使用big-endian:
>>> struct.pack('3s>I>I', b'A', 2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
struct.error: bad char in struct format
>>> struct.pack('>3sII', b'A', 2, 3)
b'A\x00\x00\x00\x00\x00\x02\x00\x00\x00\x03'
医生说:(强调我的)
By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler). Alternatively, the first character of the format string can be used to indicate the byte order, size and alignment of the packed data, according to the following table:
在https://docs.python.org/3/library/struct.html#struct-format-strings