Python struct module

Source: Internet
Author: User

First, Introduction

People who know the C language must know the role of struct structure in C, which defines a structure that contains different types of data (int, char, bool, and so on) to facilitate the processing of a structure object. In the network communication, most of the data transmitted is the binary stream (binary data) exists. When passing strings, you don't have to worry about too many problems, and when you pass basic data such as int, char, you need a mechanism to package certain struct types into a binary stream string and then transfer it over the network. The receiving end should also be able to unpack the original structure data by some mechanism. The struct module in Python provides such a mechanism, the main function of which is the conversion between the Python primitive type value and the C struct type represented in the Python string format.


Second, use

1. Basic Pack and unpack

import structimport binasciivalues =  (1,  ' ABC ',  2.7) s = struct. Struct (' i3sf ') packed_data = s.pack (*values) unpacked_data = s.unpack (packed_data) print   ' original values: ', valuesprint  ' format string : ', s.formatprint  ' USES&NBSP: ', s.size,  ' bytes ' print  ' packed value : ',  binascii.hexlify (packed_ Data) print  ' unpacked type : ',  type (unpacked_data),  '  value: ',  unpacked_data #输出Original  values:  (1,  ' abc ',  2.7)  format string : i3sf uses  : 12 bytes packed value : 0100000061626300cdcc2c40 unpacked type  : <type  ' tuple ' >  Value:  (1,  ' abc ',  2.700000047683716) 

Code, first defines a tuple of data, containing int, string, float three data types, and then defines the struct object, and has the format ' i3sf ', I means int,3s represents three character length of the string, F for float. Finally, packaging and unpacking is done through the pack and unpack of the struct. Through the output can be found that the value of the pack after the conversion to a binary byte string, and unpack can convert the byte string back to a tuple, but it is worth noting that the accuracy of float has changed, which is determined by some such as the operating system and other objective factors. The number of bytes that are consumed after packaging is very similar to the struct in C. Define format to refer to the comparison table provided by the official API:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/83/FD/wKioL1eDEBbgDJcOAADZeIeAMUI323.png "title=" 201112171613441943.png "alt=" Wkiol1edebbgdjcoaadzeieamui323.png "/>

2. Byte order

On the other hand, the post-packaged byte order is determined by the operating system by default, and of course the struct module also provides a custom byte order function that specifies the specific byte order of big-endian storage, small-end storage, and so on, which is important for the byte order of the underlying communication. Different byte order and storage methods also result in different byte sizes. Adding a specific symbol in front of the format string can represent different ways of storing bytes sequentially, for example, using small-end storage s = struct. The Struct ('<I3SF ') is available. The Official API library also provides a corresponding control list:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/FE/wKiom1eDEDGTUispAABmUan0RWg503.png "title=" 201112171613455390.png "alt=" Wkiom1ededgtuispaabmuan0rwg503.png "/>

3. Using buffer, use Pack_into and Unpack_from methods

  scenarios where binary packaging data is used are mostly performance-demanding environments. And the pack method mentioned above is to re-create a memory space for return after the input data, that is, each time we pack in memory allocated the corresponding memory resources, which is sometimes a great performance waste. The struct module also provides methods for Pack_into () and Unpack_from () to solve the problem of populating a byte with a buffer that has been allocated well in advance, instead of producing a new object to store the bytes each time.

import structimport binasciiimport ctypesvalues =   (1,  ' abc ',  2.7) s = struct. Struct (' i3sf ') prebuffer = ctypes.create_string_buffer (s.size) print  ' before : ', Binascii.hexlify (Prebuffer) S.pack_into (prebuffer,0,*values) print  ' after pack: ', binascii.hexlify ( Prebuffer) Unpacked = s.unpack_from (prebuffer,0) print  ' after unpack: ', unpacked# output before  : 000000000000000000000000 After pack: 0100000061626300cdcc2c40 After  unpack:  (1,  ' abc ',  2.700000047683716) 

Compared to packaging using the pack method, the Pack_into method has been operating on the Prebuffer object without generating unnecessary memory waste. It is also important to note that both the Pack_into and Unpack_from methods operate on a string buffer object and provide the offset parameter, allowing the user to make the appropriate processing more flexible by specifying the corresponding offset. For example, we can pack multiple objects into a buffer and then unpack by specifying different offset:

Import Structimport Binasciiimport ctypesvalues1 = (1, ' abc ', 2.7) Values2 = (' DEFG ', 101) S1 = struct. struct (' i3sf ') s2 = struct. Struct (' 4sI ') Prebuffer = Ctypes.create_string_buffer (s1.size+s2.size) print ' Before: ', Binascii.hexlify (Prebuffer) S1.pack_into (prebuffer,0,*values1) s2.pack_into (prebuffer,s1.size,*values2) print ' after Pack: ', Binascii.hexlify ( Prebuffer) Print s1.unpack_from (prebuffer,0) print S2.unpack_from (prebuffer,s1.size) #输出Before: 0000000000000000000000000000000000000000 after pack:0100000061626300cdcc2c406465666765000000 (1, ' abc ', 2.700000047683716) (' DEFG ', 101)


4.Struct.calcsize Method

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.

This article is from the "My Favorite Technology" blog, so be sure to keep this source http://svenman.blog.51cto.com/6867097/1825279

Python struct module

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.