Python struct Detailed

Source: Internet
Author: User
Tags byte sizes comparison table string back unpack

Recently in the study of Python network programming This piece, in writing a simple socket communication code, encountered the use of the struct module, it was not clear at that time this has and effect, and later consulted the relevant information about, Make a brief summary here.

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, etc.) 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 Python primitive type value and the python string format is represented by the C conversions between struct types (this module performs conversions between Python values and C structs represented as python< /c7> strings.). The Stuct module provides a very simple number of functions, which are written in a few examples below.

1. Basic Pack and unpack

The struct provides packaging and unpacking of data in format specifier (Packing and unpacking). For example:

struct struct. Struct (' i3sf ') Packed_data = S.pack (*values) Unpacked_data = S.unpack (packed_data) print ' Original values: ', Valuesprint ' Format string: ', S.formatprint ' Uses: ', s.size, ' bytes ' print ' Packed Value: ', binascii.hexlify (packed_data) print ' Unpac ked type: ', type (unpacked_data), ' Value: ', Unpacked_data

Output:

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 represents 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:

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. Struct ('<i3sf ') will do. The Official API library also provides a corresponding control list:

3. Using buffer, use Pack_into and Unpack_from methods

The scenarios in which binary packaging data are 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.

struct 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 to manipulate 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:

struct struct. Struct 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)

Output:

before:0000000000000000000000000000000000000000
After pack:0100000061626300cdcc2c406465666765000000
(1, ' abc ', 2.700000047683716)
(' DEFG ', 101)

Python struct Detailed

Related Article

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.