1 import struct # a module
2
3
4 # Functions to convert between Python values and C structs.
5 # Python bytes objects are used to hold the data representing the C struct
6 # and also as format strings (explained below) to describe the layout of data in the C struct.
7 # @: native order, size & alignment (default)
8 # =: native order, std. size & alignment
9 # <: little-endian, std. size & alignment
10 # >: big-endian, std. size & alignment
11 # !: same as >
12
13 # x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;
14 # ?: _Bool (requires C99; if not available, char is used instead)
15 # h:short; H:unsigned short; i:int; I:unsigned int;
16 # l:long; L:unsigned long; f:float; d:double; e:half-float.Special cases (preceding decimal count indicates length):
17 # s:string (array of char); p: pascal string (with count byte).Special cases (only available in native format):
18 # n:ssize_t; N:size_t;
19 # P:an integer type that is wide enough to hold a pointer.Special case (not in native mode unless ‘long long‘ in platform C):
20 # q:long long; Q:unsigned long long
21 # class Struct(object) Struct(fmt) --> compiled struct object
22 # Return a new Struct object which writes and reads binary data according to the format string fmt
23
24 # iter_unpack(...) S.iter_unpack(buffer) -> iterator(v1, v2, ...)
25 #
26 # pack_into(...) S.pack_into(buffer, offset, v1, v2, ...)
27 # unpack_from(...) S.unpack_from(buffer, offset=0) -> (v1, v2, ...)
28 #
29 # pack(...) S.pack(v1, v2, ...) -> bytes
30 # unpack(...) S.unpack(buffer) -> (v1, v2, ...)
31
32
33 import struct
34
35
36 # 1、struct.pack
37 a = 20
38 b = 400
39
40 str_ = struct.pack(">ii", a, b) # 转换后的str虽然是字符串类型,但相当于其他语言中的字节流(字节数组),可以在网络上传输
41 print(‘length:‘, len(str_))
42 print(‘type:‘, type(str_))
43 print(str_)
44 print(repr(str_))
45
46 # length: 8
47 # type: <class ‘bytes‘>
48 # b‘\x00\x00\x00\x14\x00\x00\x01\x90‘
49 # b‘\x00\x00\x00\x14\x00\x00\x01\x90‘
50
51
52 str_ = struct.pack("<ii", a, b) # 转换后的str虽然是字符串类型,但相当于其他语言中的字节流(字节数组),可以在网络上传输
53 print(‘length:‘, len(str_))
54 print(‘type:‘, type(str_))
55 print(str_)
56 print(repr(str_))
57
58 # length: 8
59 # type: <class ‘bytes‘>
60 # b‘\x14\x00\x00\x00\x90\x01\x00\x00‘
61 # b‘\x14\x00\x00\x00\x90\x01\x00\x00‘
62
63
64 # 2、struct.unpack
65 a1, a2 = struct.unpack("<ii", str_) # 返回元祖
66 print(a1, a2) # 20,400
67
68
69 # 3、struct.calcsize用于计算格式字符串所对应的结果的长度
70 print(struct.calcsize(‘>ii‘)) # 8
71
72
73 # 4、struct.pack_into, struct.unpack_from
74 from ctypes import create_string_buffer
75
76 buf = create_string_buffer(12)
77 print(repr(buf.raw)) # b‘\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00‘
78
79 struct.pack_into(">iii", buf, 0, 1, 2, -1) # 0是offset
80 print(repr(buf.raw)) # b‘\x01\x00\x00\x00\x02\x00\x00\x00\xff\xff\xff\xff‘
81
82 print(struct.unpack_from(‘>iii‘, buf, 0)) # (1, 2, -1)
83
84 # 5、struct.iter_unpack
85 ret = struct.iter_unpack(‘>iii‘, buf)
86 print(ret) # <unpack_iterator object at 0x0000000001D02E18>
87 for r in ret:
88 print(r) # (1, 2, -1)
89
90
91 # 6、练习
92 print(struct.pack(‘ci‘, b‘*‘, 0x12131415)) # b‘*\x00\x00\x00\x15\x14\x13\x12‘
93 print(struct.pack(‘ic‘, 0x12131415, b‘*‘)) # b‘\x15\x14\x13\x12*‘
94
95 record = b‘raymond \x32\x12\x08\x01\x08‘
96 print(struct.unpack(‘<10sHHb‘, record)) # (b‘raymond ‘, 4658, 264, 8)