内容简介:python struct模块
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)
| 字符 | 字节顺序 | 大小 | 对齐 |
|---|---|---|---|
| @ | 本机 | 本机 | 本机 |
| = | 本机 | 标准 | 无 |
| < | 小端 | 标准 | 无 |
| > | 大端 | 标准 | 无 |
| ! | 网络(等于大端) | 标准 | 无 |
| 格式 | C类型 | Python类型 | 标准大小 | 提示 |
|---|---|---|---|---|
| x | 填充字节 | 没有值 | ||
| c | char | 长度为1的字节 | 1 | |
| b | signed char | integer | 1 | 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 |
| n | ssize_t | integer | 4 | |
| N | size_t | integet | 4 | |
| e | 7 | float | 2 | 5 |
| f | float | float | 4 | 5 |
| d | double | float | 8 | 5 |
| s | char[] | bytes | ||
| p | char[] | bytes | ||
| P | void * | integer | 6 |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Node.js模块系统 (创建模块与加载模块)
- 黑客基础,Metasploit模块简介,渗透攻击模块、攻击载荷模块
- 022.Python模块序列化模块(json,pickle)和math模块
- 024.Python模块OS模块
- 023.Python的随机模块和时间模块
- Laravel 模块化开发模块 – Caffienate
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。