--C structure Python language conversion application in Python

struct Introduction

Struct Python provides an interface for handling structure similar to the C language.

Way of dealing with a byte structure will behave bit string, which is actually a structure.

 

The common interface struct

Mainly two, pack () and unpack ().

pack () is converted into the string structure (or byte order), unpack () is the opposite.

 

pack()

pack () function is described as follows (from the Python 2.7.15 documentation):

struct.pack(fmt, v1, v2, ...) 
Return a string containing the values v1, v2, ... packed according to the given format. 
The arguments must match the values required by the format exactly.

fmt is a special string format used to mark structure.

v1, v2 and other variables corresponding to structural body member.

It returns a string.

 

unpack()

the unpack () function is described as follows (from the Python 2.7.15 documentation):

struct.unpack(fmt, string) 
Unpack the string (presumably packed by pack(fmt, ...)) according to the given format. 
The result is a tuple even if it contains exactly one item. 
The string must contain exactly the amount of data required by the format (len(string) must equal calcsize(fmt)).

fmt with pack ().

string is the pack () conversion over a string (or byte order).

It returns a tuple, which is a structure of a member variable.

 

fmt

fmt struct interface is the most important part.

It can be divided into two parts.

The first part is a sequence of information byte and find their cis etc., represented by the following characters:

This part is not essential, if a character does not use any of the above, the same effect @ default.

Indicates the character of the second portion of the structural member of the type (which determines the number of individual members expressed need string), represented by the following characters:

 

Examples

Here is an example (using Python2.7 test OK):

import struct


if __name__ == "__main__":
    result = struct.pack('hhl', 1, 2, 3)            # result is byte string.
    for i in result:
        print '0x%02x' % ord(i)                     # 01 00 02 00 03 00 00 00
    result1 = struct.unpack ('hhl', result)         # byte string is transfered to tuple.
    print type (result1)                            # the type is tuple.
    print result1                                   # (1, 2, 3)

First Pack (), where the arguments 'hhl' is represented by three members, their types are short, short and Long; 1,2,3 values ​​thereof.

After performing a sequence of bytes returned pack () code such as comments on the right side.

Since the default size is used herein and aligned end (small end of the machine), it corresponds to 1 byte is short 0x01,0x01,2 is 0x02,0x02; 3 bytes are mapped to long 0x03,0x00,0x00 , 0x00.

After the unpack () function returns a tuple after the execution, its members are 1,2,3.

 

Guess you like

Origin blog.csdn.net/jiangwei0512/article/details/92376003