mport struct
Pack, unpack, Pack_into, Unpack_from
1 # ref:http://blog.csdn<a href= "http://lib.csdn.net/base/dotnet" class= ' Replace_word ' title= ". NET Knowledge Base "target= ' _blank ' style= ' color: #df3434; Font-weight:bold; ' >.net</a>/jgood/archive/2009/06/22/4290158.aspx 2 3 Import struct 4 5 #pack-unpack 6 Print 7 Prin T ' ===== pack-unpack ===== ' 8 9 str = struct.pack ("II", +--) print ' str: ', str-print ' Len (str): ', Len (s TR) # len (str): 8 a1, a2 = Struct.unpack ("II", str) print "A1:", A1 # A1:20 print "A2:", A2 # a2:400 print ' struct.calcsize: ', Struct.calcsize ("II") # Struct.calcsize:8 #unpack print ' ===== Unpack ===== ' (string = ' Test astring ') format = ' 5s 4x 3s ' Print struct.unpack (format, String) # (' t Est ', ' ing '), string = ' He is not very happy ', format = ' 2s 1x 2s 5x 4s 1x 5s ' Print struct.unpack (format, String) # (' He ', ' is ', ' very ', ' happy ') #pack the print ' ===== pack ===== ' 36 PNS a = [+] b = [8] str = struct.pack ("II", A, b) print ' Length: ', Len (str) #length: 43 Print str Print repr (str) # '/x14/x00/x00/x00/x90/x01/x00/x00 ' in #pack_into-unpack_from print ' ===== PAC K_into-unpack_from ===== ' from ctypes import create_string_buffer (buf = create_string_buffer) Print Repr (Buf.raw) Struct.pack_into ("III", BUF, 0, 1, 2,-1) Print repr (Buf.raw) ("III", BUF, 0)
Operation Result:
[Email protected] python]$ Python struct_pack.py
===== Pack-unpack =====
Str:?
Len (str): 8
A1:20
a2:400
Struct.calcsize:8
===== Unpack =====
(' test ', ' ing ')
(' He ', ' is ', ' very ', ' happy ')
===== Pack =====
Length:8
?
'/x14/x00/x00/x00/x90/x01/x00/x00 '
===== Pack_into-unpack_from =====
'/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00 '
'/x01/x00/x00/x00/x02/x00/x00/x00/xff/xff/xff/xff '
(1, 2,-1)
Python is a very concise language, and for data type representations, many types are not predefined like in other languages (e.g., in C #, there are 8 types of optical integers defined)
It only defines six basic types: string, integer, floating point, tuple (set), List (array), dictionary (Key/value)
With these six data types, we can do most of the work. But when Python needs to interact with other platforms over the network, it must take into account the conversion of these data types to the types of other platforms or languages. For example: the C + + write client sends an INT (4 bytes) variable data to a Python-written server, and Python receives 4 bytes of data representing the integer, how to parse it into a python-recognized integer? Python's standard module struct is used to solve this problem.
The content of the struct module is not too much, it is not too difficult, the following is the most commonly used methods are described:
1, Struct.pack
Struct.pack is used to convert Python values from a format character to a string (because there is no byte type in Python , you can interpret the string here as a byte stream, or an array of bytes). The function prototypes are: Struct.pack (FMT, V1, v2, ...), the parameter FMT is a format string, and the relevant information about the format string is described below. V1, v2, ... Represents the Python value to convert. The following example converts two integers to a string (byte stream):
1 #!/usr/bin/env python 2 #encoding: UTF8 3 4 Import sys 5 reload (SYS) 6 sys.setdefaultencoding (" Utf-8 ") 7 8 import struct 9 a = ten B = str = struct.pack (" II ", A, b) print ' Length: ', Len (str) # Length: 8 print str # garbled: print repr (str) # ' \x14\x00\x00\x00 \x90\x01\x00\x00 '
The format character "I" represents the conversion to int, ' II ' means there are two int variables.
The result of the conversion is 8 bytes long (the int type takes 4 bytes and two int is 8 bytes)
You can see that the result of the output is garbled, because the result is binary data, so it is displayed as garbled.
You can use Python's built-in function repr to get a recognizable string, where 16 binary 0x00000014, 0x00001009 20 and 400, respectively.
2, Struct.unpack
Struct.unpack does work just as opposed to struct.pack, which is used to convert byte streams into Python data types. The function prototype is: Struct.unpack (FMT, String), which returns a tuple.
The following is a simple example:
1 #!/usr/bin/env python 2 #encoding: UTF8 3 4 Import sys 5 reload (SYS) 6 sys.setdefaultencoding (" Utf-8 ") 7 8 import struct 9 a = ten B = + /-pack + str = struct.pack (" II " , A, b) print ' Length: ', Len (str) # Length: 8 print str # garbled: print repr (str)
# ' \x14\x00\x00\x00\x90\x01\x00\x00 ' # unpack str2 = Struct.unpack ("II", str) print ' Length: ', Len (str2) # Length: 2 print str2 # (+, +) print repr (str2) # (20, 400)
3, Struct.calcsize
The struct.calcsize is used to calculate the length of the result of the format string, such as: Struct.calcsize (' II '), which returns 8. Because two int types occupy a length of 8 bytes.
1 import struct 2 print "Len:", struct.calcsize (' I ') # len: 4 3 print "Len:", Struct.calcsize (' II ')
# Len: 8 4 print "Len:", Struct.calcsize (' F ') # len: 4 5 print "Len:", struct.calcsize (' FF ') # len: 8 6 print "Len:", Struct.calcsize (' s ') # len: 1 7 print "Len:", Struct.calcsize ( ' SS ') # len: 2 8 print "Len:", Struct.calcsize (' d ') # len: 8 9 print "Len:", Struct.calcsize (' DD ') # len:
4, Struct.pack_into, Struct.unpack_from
These two functions are described in the Python manual, but there are no examples of how to use them. In fact, they do not use much in practical applications. Google for a long time, only to find an example, paste out to share a bit:
1 #!/usr/bin/env python 2 #encoding: UTF8 3 4 Import sys 5 reload (SYS) 6 sys.setdefaultencoding (" Utf-8 ") 7 8 import struct 9 from ctypes import create_string_buffer buf = create_string_ Buffer (14) print repr (buf.raw) # ' \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 ' Struct.pack_into ("III", BUF, 0, 1, 2,-1) print repr (buf.raw) # ' \x01\x00\x00\x00\x02\x00\x00\x00\xff\xff \xff\xff ' print Struct.unpack_from ("III", BUF, 0)
struct Type table
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 |
1 |
|
P |
Char[] |
String |
|
|
P |
void * |
Integer |
|
(5), (3) |
Pack, unpack of the Python:struct module