Excerpt from: https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001431955007656a66f831e208e4c189b8a9e9f3f25ba53000
Python provides a struct
module to resolve bytes
conversions to and from other binary data types.
struct
function to turn any data type into bytes
: pack
Import struct>>> struct.pack ('>i', 10240099) b' \x00\[email protected]'
pack
The first parameter is the processing instruction, ‘>I‘
which means:
>
Indicates that the byte order is Big-endian, which is I
the network order , which represents a 4-byte unsigned integer .
The number of parameters to be followed is the same as the processing instructions.
unpack
Turn it bytes
into the appropriate data type :
>>> struct.unpack ('>ih', b'\xf0\xf0\xf0\xf0\x80\x80 ') (4042322160, 32896)
According >IH
to the instructions, the bytes
following changes I
to:4-byte unsigned integers and H
: An 2-byte unsigned integer .
So, although Python is not a good place to write code for the underlying operation of the byte stream , it is much easier to use in places where performance requirements are low struct
.
struct
The data types defined by the module can refer to the official Python documentation:
Https://docs.python.org/3/library/struct.html#format-characters
The bitmap file (. bmp) of Windows is a very simple file format that we use to struct
analyze.
First find a BMP file, no words with "paint" to draw one.
Read in the first 30 bytes to parse:
>>> s = b'\x42\x4d\x38\x8c\x0a\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00\x00\x00\x80\ x02\x00\x00\x68\x01\x00\x00\x01\x00\x18\x00'
BMP format uses a small-end way to store data, the structure of the file header in order as follows:
Two bytes: ' BM ' represents a Windows bitmap, ' BA ' represents a OS/2 bitmap, a 4-byte integer: Represents the bitmap size, a 4-byte integer: Reserved bit, always 0, and a 4-byte integer: The offset of the actual image; a 4-byte integer: The number of bytes in the header ; a 4-byte integer: Image width; a 4-byte integer: Image height; a 2-byte integer: Always 1; a 2-byte integer: The number of colors.
So, combine them with unpack
read:
>>> struct.unpack ('<cciiiiiihh', s) (b'b') , b'M', 691256, 0, 54, 40, 640, 360, 1, 24)
Python Learning notes (35) struct