Interpret bytes as packed binary data

 

 

7.1. struct — Interpret bytes as packed binary data — Python 3.6.5 documentation https://docs.python.org/3/library/struct.html

 

ource code: Lib/struct.py


This module performs conversions between Python values and C structs represented as Python bytes objects. This can be used in handling binary data stored in files or from network connections, among other sources. It uses Format Strings as compact descriptions of the layout of the C structs and the intended conversion to/from Python values.

Note

 

By default, the result of packing a given C struct includes pad bytes in order to maintain proper alignment for the C types involved; similarly, alignment is taken into account when unpacking. This behavior is chosen so that the bytes of a packed struct correspond exactly to the layout in memory of the corresponding C struct. To handle platform-independent data formats or omit implicit pad bytes, use standard size and alignment instead of nativesize and alignment: see Byte Order, Size, and Alignment for details.

Several struct functions (and methods of Struct) take a buffer argument. This refers to objects that implement theBuffer Protocol and provide either a readable or read-writable buffer. The most common types used for that purpose are bytes and bytearray, but many other types that can be viewed as an array of bytes implement the buffer protocol, so that they can be read/filled without additional copying from a bytes object.

 

posted @ 2018-06-06 17:02  papering  阅读(264)  评论(0编辑  收藏  举报