Usage of struct. pack () and struct. unpack () in Python

Source: Internet
Author: User
The following code {code...} is available ...} the results (& amp; lt; typeclassobj & amp; gt;, & amp; lt; typeinstance & amp; gt;) obtained by Python2.7.11Python3.5.1 in python2 are two python versions respectively ;) a is a class object, and a1 is the result of an instance in python3 & amp; lt; classt... 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)

As the name suggests, unpack. 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). the description of these format characters is as follows:

2. Sample code

import struct # native byteorder buffer = struct.pack("ihb", 1, 2, 3) print repr(buffer) print struct.unpack("ihb", buffer) # data from a sequence, network byteorder data = [1, 2, 3] buffer = struct.pack("!ihb", *data)print repr(buffer) print struct.unpack("!ihb", buffer) Output:'\x01\x00\x00\x00\x02\x00\x03'(1, 2, 3)'\x00\x00\x00\x01\x00\x02\x03'(1, 2, 3)

First, package the parameters 1, 2, 3, and the first 1, 2, and 3 are obviously integers in the python data type. after the pack, it becomes 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:

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.

The above is a detailed description of the usage of struct. pack () and struct. unpack () in Python. For more information, see other related articles in the first PHP community!

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.