Python struct module, pythonstruct
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 & alignment11 #!: 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; Q: unsigned long long21 # class Struct (object) Struct (fmt) --> compiled struct object22 # Return a new Struct object which writes and reads binary data according to the format string fmt23 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 ,...) -> bytes30 # unpack (...) s. unpack (buffer)-> (v1, v2 ,...) 31 32 33 import struct34 35 36 #1. struct. pack37 a = 2038 B = 40039 40 str _ = struct. pack ("> ii", a, B) # although the converted str is of the string type, it is equivalent to the byte stream (byte array) in other languages ), 41 print ('length: ', len (str _) 42 print ('Type:', type (str _) 43 print (str _) can be transmitted over the network _) 44 print (repr (str _) 45 46 # length: 847 # type: <class 'bytes '> 48 # B' \ x00 \ x00 \ x00 \ x14 \ x00 \ x00 \ x01 \ x90' 49 # B '\ x00 \ x00 \ x00 \ x00 \ x14 \ x00 \ x00 \ x01 \ x90 '50 51 52 str _ = struct. pack ("<ii", a, B) # although the converted str is of the string type, it is equivalent to the byte stream (byte array) in other languages ), 53 print ('length: ', len (str _) 54 print ('Type:', type (str _) 55 print (str _) can be transmitted over the network _) 56 print (repr (str _) 57 58 # length: 859 # type: <class 'bytes '> 60 # B' \ x14 \ x00 \ x00 \ x00 \ x90 \ x01 \ x00 \ x00 '61 # B '\ x14 \ x00 \ x00 \ x00 \ x00 \ x90 \ x01 \ x00 \ x00 '62 63 64 #2. struct. unpack65 a1, a2 = struct. unpack ("<ii", str _) # returned ancestor 66 print (a1, a2) #20, 40067 68 69 #3, struct. calcsize is used to calculate the result length 70 print (struct. calcsize ('> II') #871 72 73 #4. struct. pack_into, struct. unpack_from74 from ctypes import create_string_buffer75 76 buf = create_string_buffer (12) 77 print (repr (buf. raw) # B '\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 '78 79 struct. pack_into ("> iii", buf, 0, 1, 2,-1) #0 is offset80 print (repr (buf. raw) # B '\ x01 \ x00 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00 \ xff '81 82 print (struct. unpack_from ('> II', buf, 0) # (1, 2,-1) 83 84 #5, struct. iter_unpack85 ret = struct. iter_unpack ('> II', buf) 86 print (ret) # <unpack_iterator object at 0x0000000001D02E18> 87 for r in ret: 88 print (r) # (1, 2, -1) 89 90 91 #6. Exercise 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)
| Character |
Byte order |
Size |
Alignment |
| @ |
Local Machine |
Local Machine |
Local Machine |
| = |
Local Machine |
Standard |
None |
| < |
Small Terminal |
Standard |
None |
| > |
Big End |
Standard |
None |
| ! |
Network (equal to large end) |
Standard |
None |
| Format |
C type |
Python type |
Standard size |
Prompt |
| X |
Fill byte |
No value |
|
|
| C |
Char |
1 byte |
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 |
Integer |
8 |
2, 3 |
| Q |
Unsigned 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 |