在使用python进行二进制数据处理的时候,需要用到pack和unpack。
在pack/unpack 的参数中,除了表示格式的字母或符号,还会出现数字,通过查阅python的官方文档,记录如下
A format character may be preceded by an integral repeat count. For example, the format string '4h'
means exactly the same as 'hhhh'
. 字母前的数字表示重复该字母多少次。
Whitespace characters between formats are ignored; a count and its format must not contain whitespace though.
For the 's'
format character, the count is interpreted as the length of the bytes, not a repeat count like for the other format characters; for example, '10s'
means a single 10-byte string, while '10c'
means 10 characters. If a count is not given, it defaults to 1. For packing, the string is truncated or padded with null bytes as appropriate to make it fit. For unpacking, the resulting bytes object always has exactly the specified number of bytes. As a special case, '0s'
means a single, empty string (while '0c'
means 0 characters). 对于字符串string 和字符char,前面的数字是由差别的。string前的数字表明该string的长度。而char前的数字表示重复char多少次。
详情请参考 python 官方文档。https://docs.python.org/3/library/struct.html