Python module introduction-conversion of binary and string data in struct binary data structure
Magnet
# Undertake software automation implementation, training, and other gtalk: ouyangchongwu # gmail. comqq 37391319
# Copyright. For reposted content, please contact us by letter
# Shenzhen testing automation python project receiving group 113938272 Shenzhen accounting software testing part-time job 6089740
# Shenzhen dashboard group 66250781 wugang DongKou Chengbu Xinning township sentiment group 49494279
# Reference: The PythonStandard Libraryby Example 2011
Converts byte data and python data types. The format indicator has the same mutations mechanism as the re module. Therefore, creating a Struct instance and calling an instance method is more effective than using a module function.
Package and package:
File: struct_pack.py
Import binascii
Values = (1, 'AB', 2.7)
S = struct. Struct ('I 2 s ')
Packed_data = s. pack (* values)
Print 'original values: ', values
Print 'format string: ', s. Format
Print 'uses: ', s. size, 'bytes'
Print 'packed Value: ', binascii. hexlify (packed_data)
Execution result:
Original values: (1, 'AB', 2.7)
Format string: I 2 s f
Uses: 12 bytes
Packed Value: 000000061620000cdcc2c40
File: struct_unpack.py
Import struct
Import binascii
Packed_data = binascii. unhexlify ('000000061620000cdcc2c40 ')
S = struct. Struct ('I 2 s ')
Unpacked_data = s. unpack (packed_data)
Print 'unpacked Values: ', unpacked_data
Execution result:
Unpacked Values: (1, 'AB', 2.700000047683716)
Byte order:
Import struct
Import binascii
Values = (1, 'AB', 2.7)
Print 'original values: ', values
Endianness = [
('@', 'Native, native '),
('=', 'Native, standard '),
('<', 'Little-endian '),
('>', 'Big-endian '),
('! ', 'Network '),
]
For code, name in endianness:
S = struct. Struct (code + 'i2s F ')
Packed_data = s. pack (* values)
Print
Print 'format string: ', s. Format,' for ', name
Print 'uses: ', s. size, 'bytes'
Print 'packed Value: ', binascii. hexlify (packed_data)
Print 'unpacked Value: ', s. unpack (packed_data)
Execution result:
Original values: (1, 'AB', 2.7)
Format string: @ I 2 s f fornative, native
Uses: 12 bytes
Packed Value: 000000061620000cdcc2c40
Unpacked Value: (1, 'AB', 2.700000047683716)
Format string: = I 2 s f fornative, standard
Uses: 10 bytes
Packed Value: 010000006162cdcc2c40
Unpacked Value: (1, 'AB', 2.700000047683716)
Format string: <I 2 s f forlittle-endian
Uses: 10 bytes
Packed Value: 010000006162cdcc2c40
Unpacked Value: (1, 'AB', 2.700000047683716)
Format string:> I 2 s f forbig-endian
Uses: 10 bytes
Packed Value: 000000016162402 ccccd
Unpacked Value: (1, 'AB', 2.700000047683716)
Format string :! I 2 s f fornetwork
Uses: 10 bytes
Packed Value: 000000016162402 ccccd
Unpacked Value: (1, 'AB', 2.700000047683716)
When performance requirements are high, you can use buffer. pack_into () and unpack_from () can directly interact with buffer.
Import struct
Import binascii
S = struct. Struct ('I 2 s ')
Values = (1, 'AB', 2.7)
Print 'original: ', values
Print
Print 'ctypes string buffer'
Import ctypes
B = ctypes. create_string_buffer (s. size)
Print 'before: ', binascii. hexlify (B. raw)
S. pack_into (B, 0, * values)
Print 'after: ', binascii. hexlify (B. raw)
Print 'unpacked: ', s. unpack_from (B, 0)
Print
Print 'array'
Import array
A = array. array ('C', '\ 0' * s. size)
Print 'before: ', binascii. hexlify ()
S. pack_into (a, 0, * values)
Print 'after: ', binascii. hexlify ()
Print 'unpacked: ', s. unpack_from (a, 0)
Execution result:
Original: (1, 'AB', 2.7)
Ctypes string buffer
Before: 000000000000000000000000
After: 0100000061620000cdcc2c40
Unpacked: (1, 'AB', 2.700000047683716)
Array
Before: 000000000000000000000000
After: 0100000061620000cdcc2c40
Unpacked: (1, 'AB', 2.700000047683716)
The Struct module can work with namedtuple to make binary data more readable:
>>> Record = 'raymond \ x32 \ x12 \ x08 \ x01 \ x08'
>>> Name, serialnum, school, gradelevel = unpack ('<10sHHb', record)
>>> From collections import namedtuple
>>> Student = namedtuple ('student ', 'name serialnum schoolgradelevel ')
>>> Student. _ make (unpack ('<10sHHb', record ))
Student (name = 'raymond ', serialnum = 4658, school = 264, gradelevel = 8)
Similar modules: xdrlib, array