Python learning-pack and unpack examples of struct module

Source: Internet
Author: User
Tags unpack

Import struct

Pack, unpack, pack_into, unpack_from

 

# Ref: http://blog.csdn.net/JGood/archive/2009/06/22/4290158.aspx </P> <p> Import struct </P> <p> # Pack-Unpack <br/> Print '= pack-Unpack =====' </P> <p> STR = struct. pack ("II", 20,400) <br/> Print 'str: ', STR <br/> Print 'len (STR):', Len (STR) # Len (STR): 8 </P> <p> A1, A2 = struct. unpack ("II", STR) <br/> Print "A1:", A1 # A1: 20 <br/> Print "A2:", A2 # A2: 400 </P> <p> Print 'struct. calcsize: ', struct. calcsize ("II") # struct. calcsize: 8 </P> <p> # unpack <br/> Print '==== unpack ====' </P> <p> string = 'test astring' <br/> Format = '5s 4x 3s '<br/> Print struct. unpack (format, string) # ('test', 'in ') </P> <p> string = 'He is not very happy '<br/> Format = '2s 1X2 S 5x 4S 1x 5s' <br/> Print struct. unpack (format, string) # ('hes', 'is ', 'very', 'happy ') </P> <p> # Pack <br/> Print '==== pack ====' </P> <p> = 20 <br/> B = 400 </P> <p> STR = struct. pack ("II", a, B) <br/> Print 'length: ', Len (STR) # length: 8 <br/> Print STR <br/> Print repr (STR) # '/x14/x00/x00/x00/x90/x01/x00/x00' </P> <p> # pack_into-unpack_from <br/> Print <br/> '==== pack_into-unpack_from ====' <br/> from ctypes import create_string_buffer </P> <p> Buf = create_string_buffer (12) <br/> Print repr (BUF. raw) </P> <p> struct. pack_into ("III", Buf, 0, 1, 2,-1) <br/> Print repr (BUF. raw) </P> <p> Print struct. unpack_from ("III", Buf, 0) <br/>

 

 

Running result:

[Work@db-testing-com06-vm3.db01.baidu.com Python] $ Python struct_pack.py

 

===== Pack-Unpack ====
STR :?
Len (STR): 8
A1: 20
A2: 400
Struct. calcsize: 8

 

===== Unpack =====
('Test', 'in ')
('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'
(1, 2,-1)

 

========================================================== ==============================================

 

Python is a very concise language, and its representation of data types is not as predefined in other languages (for example, in C #, 8 types of optical integers are defined ), it only defines six basic types: String, integer, floating point, tuples, lists, and dictionaries. With these six data types, We can do most of our work. However, when Python needs to interact with other platforms through the network, it is necessary to consider the issue of converting these data types with those of other platforms or languages. For example, a C ++ client sends an int-type (4-byte) variable data to a python-written server, and Python receives four bytes of data that represents this integer, how can I parse it into an integer recognized by python? The standard Python module struct is used to solve this problem.

The struct module does not have much content and is not too difficult. The following describes the most common methods:

Struct. Pack
Struct. pack is used to convert Python values to strings Based on format characters (because Python does not have the byte type, you can understand the strings here as byte streams or byte arrays ). The function prototype is struct. Pack (FMT, V1, V2,...). The FMT parameter is a format string. The information about the format string is described below. V1, V2,... indicates the python value to be converted. The following example converts two integers to a string (byte stream ):

Import struct

A = 20
B = 400

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 and can be transmitted over the network.
Print 'length: ', Len (STR)
Print Str
Print repr (STR)

# ---- Result
# Length: 8
# ---- Garbled characters
# '/X14/x00/x00/x00/x90/x01/x00/x00'
Import struct

A = 20
B = 400

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 and can be transmitted over the network.
Print 'length: ', Len (STR)
Print Str
Print repr (STR)

# ---- Result
# Length: 8
# ---- Garbled characters
# '/X14/x00/x00/x00/x90/x01/x00/x00'

The format character "I" indicates the conversion to int, and 'II' indicates that there are two int variables. The length of the converted result is 8 bytes (the int type occupies 4 bytes and the two int values are 8 bytes). The output result is garbled because the result is binary data, so it is garbled. You can use the python built-in function repr to obtain recognizable strings. The hexadecimal 0x00000014 and 0x00001009 indicate 20 and 400 respectively.

Struct. Unpack
Struct. Unpack is the opposite of struct. Pack. It is used to replace byte Transfer with Python data type. Its function prototype is struct. Unpack (FMT, string). This function returns a tuples. The following is a simple example: Str = struct. Pack ("II", 20,400)
A1, a2 = struct. Unpack ("II", STR)
Print 'a1: ', A1
Print 'a2: ', A2

# ---- Result:
# A1: 20
# A2: 400
STR = struct. Pack ("II", 20,400)
A1, a2 = struct. Unpack ("II", STR)
Print 'a1: ', A1
Print 'a2: ', A2

# ---- Result:
# A1: 20
# A2: 400
 

Struct. calcsize
Struct. calcsize is used to calculate the length of the result corresponding to the format string, for example, struct. calcsize ('II'). 8 is returned. Because the length of two int types is 8 bytes.

Struct. pack_into, struct. unpack_from
These two functions are described in the python manual, but no examples are provided for use. In fact, they are rarely used in practical applications. After Google for a long time, I found an example and posted it for sharing:

Import struct
From ctypes import create_string_buffer

Buf = create_string_buffer (12)
Print repr (BUF. Raw)

Struct. pack_into ("III", Buf, 0, 1, 2,-1)
Print repr (BUF. Raw)

Print struct. unpack_from ('II', Buf, 0)

# ---- Result
# '/X00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00'
# '/X01/x00/x00/x00/x02/x00/x00/x00/xFF'
# (1, 2,-1)
Import struct
From ctypes import create_string_buffer

Buf = create_string_buffer (12)
Print repr (BUF. Raw)

Struct. pack_into ("III", Buf, 0, 1, 2,-1)
Print repr (BUF. Raw)

Print struct. unpack_from ('II', Buf, 0)

# ---- Result
# '/X00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00'
# '/X01/x00/x00/x00/x02/x00/x00/x00/xFF'
# (1, 2,-1)

For details, refer to the python manual struct module.

Python manual struct module: http://docs.python.org/library/struct.html#module-struct

 

Reprinted statement:This article from http://blog.csdn.net/JGood/archive/2009/06/22/4290158.aspx

 

 

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)

 

Notes:

  1. The'? 'Conversion Code corresponds to_ BoolType defined by c99. if this type is not available, it is simulated usingChar. In standard mode, it is always represented by one byte.

    New in version 2.6.

  2. the 'q' and 'q' conversion codes are available in native mode only if the platform C compiler supports C long long , or, on Windows, __ int64 . they are always available in standard modes.

    New in version 2.2.

  3. when attempting to pack a non-integer using any of the Integer Conversion codes, if the non-integer has a __ index __() method then that method is called to convert the argument to an integer before packing. if no __ index _ () method exists, or the call to __ index __() raises typeerror , then the __ int _ () method is tried. however, the use of __ int _ () is deprecated, and will raise deprecationwarning .

    Changed in version 2.7:Use of_ Index __()Method for non-integers is new in 2.7.

    Changed in version 2.7:Prior to version 2.7, not all Integer Conversion codes wocould use_ Int __()Method to convert, andDeprecationwarningWas raised only for float arguments.

  4. for the 'F' and 'D' conversion codes, the packed representation uses the IEEE 754 binary32 (for 'F' ) or binary64 (for 'D' ) format, regardless of the floating-point format used by the platform.

  5. The'P'Format character is only available for the native byte ordering (selected as the default or with'@'Byte order character). The byte order character'='Chooses to use little-or big-Endian Ordering Based on the host system. The struct module does not interpret this as native ordering, so'P'Format is not available.

A format character may be preceded by an integral repeat count. For example, the Format String'4h'Means exactly the same'Hhhh'.

Whitespace characters between formats are ignored; a count and its format must not contain whitespace though.

For'S'Format character, the count is interpreted as the size of the string, not a repeat count like for the other format characters; for example,'10s'Means a single 10-byte string, while'10c'Means 10 characters. for packing, the string is truncated or padded with NULL bytes as appropriate to make it fit. for unpacking, the resulting string always has exactly the specified number of bytes. as a special case,'0s'Means a single, empty string (while'0C'Means 0 characters ).

The'P' Format character encodes a "Pascal string", meaning a short variable-length string stored in Fixed number of bytes , Given by the count. The first byte stored is the length of the string, or 255, whichever is smaller. The bytes of the string follow. If the string passed in Pack () Is too long (longer than the Count minus 1), only the leading Count-1 Bytes of the string are stored. If the string is shorter Count-1 , It is padded with NULL bytes so that exactly count bytes in all are used. Note that Unpack () , 'P' Format character consumes count bytes, but that the string returned can never contain more than 255 characters.

For'P'Format character, the return value is a python integer or long integer, depending on the size needed to hold a pointer when it has been cast to an integer type.NullPointer will always be returned as the python integer0. When packing pointer-sized values, Python integer or long integer objects may be used. for example, the Alpha and Merced processors use 64-bit pointer values, meaning a python long integer will be used to hold the pointer; other platforms use 32-bit pointers and will use a python integer.

for the '? ' Format character, the return value is either true or false . when packing, the truth value of the argument object is used. either 0 or 1 in the native or standard bool representation will be packed, and any non-zero value will be true when unpacking.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.