pack和unpack在處理二進(jìn)制流中比較常用的封包、解包格式
# 按照給定的格式(fmt),把數(shù)據(jù)封裝成字符串(實際上是類似于c結(jié)構(gòu)體的字節(jié)流)
pack(fmt, v1, v2, ...)
# 按照給定的格式(fmt)解析字節(jié)流string,返回解析出來的tuple
unpack(fmt, string)
# 計算給定的格式(fmt)占用多少字節(jié)的內(nèi)存
calcsize(fmt)
fmt格式為:
FORMAT | C TYPE | PYTHON TYPE | STANDARD SIZE | NOTES |
x | pad byte | no value | | |
c | char | string of length 1 | 1 | |
b | signed char | integer | 1 | (3) |
B | unsigned char | integer | 1 | (3) |
? | _Bool | bool | 1 | (1) |
h | short | integer | 2 | (3) |
H | unsigned short | integer | 2 | (3) |
i | int | integer | 4 | (3) |
I | unsigned int | integer | 4 | (3) |
l | long | integer | 4 | (3) |
L | unsigned long | integer | 4 | (3) |
q | long long | integer | 8 | (2), (3) |
Q | unsigned long long | integer | 8 | (2), (3) |
f | float | float | 4 | (4) |
d | double | float | 8 | (4) |
s | char[] | string | | |
p | char[] | string | | |
P | void * | integer | | (5), (3) |
格式中的第一個字符來改變對齊方式(字節(jié)序).定義如下
CHARACTER | BYTE ORDER | SIZE | ALIGNMENT |
@ | native | native | native |
= | native | standard | none |
< | little-endian | standard | none |
> | big-endian | standard | none |
! | network (= big-endian) | standard | none |
例如:數(shù)據(jù)格式為:
unsigned short id;
char[4] tag;
unsigned int version;
unsigned int count;
解包:
import struct
id, tag, version, count = struct.unpack("!H4s2I", s)
封包:
ss = struct.pack("!H4s2I", id, tag, version, count);
例如:格式為:2字節(jié)(包長度)+4字節(jié)(包id)+包內(nèi)容 >表示字節(jié)序
解包
size, = struct.unpack('>H',raw[0:2])
cmd, = struct.unpack('>H', raw[2:4])
string, = struct.unpack('>{0}s'.format(size - 4), raw[4:size])
封包:
fmt = ">HH{0}s".format(len(result))
args = (len(result), cmd,result)
data = struct.pack(fmt, *args)