In python, the struct. pack () and struct. unpack () functions, structpackunpack

Source: Internet
Author: User
Tags unpack

In python, the struct. pack () and struct. unpack () functions, structpackunpack

Struct in python is mainly used to process C-structure data. It is first converted to a Python string type during reading, and then to a Python structured type, such as a tuple ~. Generally, the input channel comes from the binary stream of a file or network.

 1. struct. pack () and struct. unpack ()

In the conversion process, a format strings is used to specify the conversion method and format.

The following describes the main methods:

1.1 struct. pack (fmt, v1, v2 ,.....)

Package the values of parameters such as v1 and v2. The packaging method is specified by fmt. The encapsulated parameters must strictly comply with fmt. Returns a wrapped string.

1.2 struct. unpack (fmt, string)

Take care of your name and unpack the package. For example, pack the package and unpack the package. Returns a tuple obtained from the unwrapped data (string). Even if only one data is returned, it is unwrapped into a metagroup. Len (string) must be equal to calcsize (fmt), which involves a calcsize function. Struct. calcsize (fmt): This is used to calculate the size of the structure described in the fmt Format.

A format string is composed of one or more format characters (format characters ).

Format C Type Python Note
X Pad byte No value  
C Char String of length 1  
B Signedchar Integer  
B Unsignedchar Integer  
? _ Bool Bool (1)
H Short Integer  
H Unsignedshort Integer  
I Int Integer  
I Unsignedint Integer or long  
L Long Integer  
L Unsignedlong Long  
Q Longlong Long (2)
Q Unsignedlonglong Long (2)
F Float Float  
D Double Float  
S Char [] String  
P Char [] String  
P Void * Long  

 

2. Sample Code

 1 import struct  2  3 # native byteorder  4 buffer = struct.pack("ihb", 1, 2, 3)  5 print repr(buffer)  6 print struct.unpack("ihb", buffer)  7  8 # data from a sequence, network byteorder  9 data = [1, 2, 3] 10 buffer = struct.pack("!ihb", *data)11 print repr(buffer) 12 print struct.unpack("!ihb", buffer)13 14  15 16  17 18 Output:19 20 '\x01\x00\x00\x00\x02\x00\x03'21 (1, 2, 3)22 '\x00\x00\x00\x01\x00\x02\x03'23 (1, 2, 3)
View Code

First, package the parameters 1, 2, 3, and the first 1, 2, and 3 are obviously integers in the python data type. pack the data into a binary string of the C structure, convert the string type to '\ x01 \ x00 \ x00 \ x00 \ x02 \ x00 \ x03 '. Because the local machine is a small terminal ('little-endian '), please refer to the differences between the big end and the small end here, so the high point is placed in the low address segment. I Represents the int type in C struct, so the local machine occupies 4 places, 1 represents 01000000; h Represents the short type in C struct, occupies 2 places, so it represents 0200; similarly, B Represents the signed char type in C struct, which occupies 1 and thus represents 03.

The conversion of other structures is similar. For some special information, see Manual in the official document.

At the top of the Format string, there is an optional character to determine the big end and small end, the list is as follows:

     
@ Native Native
= Native Standard
< Little-endian Standard
> Big-endian Standard
! Network (= big-endian) Standard

If no append exists, the default value is @, that is, the local character sequence (large or small) is used. The size of the C structure and the alignment in the memory are also consistent with those in the Local Machine (native ), for example, some machines have two integers, while some machines have four digits. Some machines have four alignment for memory, while others are n-bit alignment (n unknown, I don't know how much ).

There is also a standard option, which is described as: if the standard option is used, no memory alignment is available for any type.

For example, in the second half of the applet, the first digit in the format string used is !, That is, the standard alignment mode of the big end mode. Therefore, the output is '\ x00 \ x00 \ x00 \ x01 \ x00 \ x02 \ x03 ', in this case, you can put the high address in the memory.

  

  

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.