Python Learning's struct module

Source: Internet
Author: User

class struct. Struct (format)

Returns a struct object (struct, reference c).

The object can read and write binary data according to the format of the formatted string.

The first parameter formats a string to specify the order of the bytes.

By default, it is determined by the system, and it is provided in a custom way, just by adding a specific character to the front:

struct. Struct ('>i4sf')

The specific character comparison table is attached.

Common Methods and properties:

Struct.pack (FMT, V1, v2, ...)

Returns a byte stream object.

Package parameter V1,v2 in the format of FMT (formatted string),...。

The popular saying is:

First, the different types of data objects are placed in a "group" (e.g. tuples (1, ' good ', 1.22)),

Then package ("group" is converted to a byte stream object), and then unpack (convert the byte stream object to "group").

format

Format the string.

size

The size of the structure body.

Instance:

1. Usual packaging and unpacking

#-*-Coding:utf-8-*-"" "Package and Package" "" Import structimport binasciivalues = (1, b ' good ', 1.22) #查看格式化字符串可知, the string must be a byte stream type. s = struct. Struct (' i4sf ') Packed_data = S.pack (*values) Unpacked_data = S.unpack (packed_data) print (' Original values: ', values) Print (' Format string: ', S.format) print (' Uses: ', s.size, ' bytes ') print (' Packed Value: ', binascii.hexlify (Packed_data) Print (' unpacked type: ', type (unpacked_data), ' Value: ', Unpacked_data)

Results:

Original Values: (1B'Good',1.22) Formatstring: b'I4SF'Uses: Abytespacked value:b'01000000676f6f64f6289c3f'unpacked Type:<class 'tuple'> Value: (1B'Good',1.2200000286102295) [Finishedinch 0.1s]

Description

The data object is first placed in a tuple, then a struct object is created, and the tuple is packaged using the pack () method, and the last solution returns the tuple.

The binascii.hexlify (data) function is used here.

Binascii.hexlify (data)

Returns the hexadecimal representation of the binary data.

2. Use buffer for packaging and unpacking

#-*-Coding:utf-8-*-"" "Package and package by buffer mode" "" Import structimport binasciiimport ctypesvalues = (1, b ' good ', 1.22) #查看格式化字符串 The string must be a byte stream type. s = struct. Struct (' i4sf ') buff = Ctypes.create_string_buffer (s.size) Packed_data = S.pack_into (buff,0,*values) Unpacked_data = S.unpack_from (buff,0) print (' Original values: ', values) print (' Format string: ', S.format) print (' Buff: ', buff) print (' Packed Value: ', binascii.hexlify (Buff)) print (' Unpacked type: ', type (unpacked_data), ' Value: ', Unpacked_data)

Results:

Original Values: (1B'Good',1.22) Formatstring: b'I4SF'Buff:<ctypes.c_char_array_12ObjectAt0x00000008a8c27348>Packed value:b'01000000676f6f64f6289c3f'unpacked Type:<class 'tuple'> Value: (1B'Good',1.2200000286102295) [Finishedinch 0.1s]

Description

Report:

1. Formatting tables

2. Byte order, size and calibration

Python Learning's 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.