Introduction to the struct module in Python
This article mainly introduces the struct module in Python. The Code is based on Python2.x. For more information, see
To be precise, Python does not specifically process byte data types. However, because str is both a string and can represent bytes, the byte array is str. In the C language, we can easily use struct and union to process bytes, as well as the conversion of bytes, int, and float.
In Python, for example, to convert a 32-bit unsigned integer into a byte, that is, four-length str, you must use the bitwise operator to write it like this:
?
|
1
2
3
4
5
6
7
8 |
>>> N = 10240099
>>> B1 = chr (n & 0xff000000)> 24)
>>> B2 = chr (n & 0xff0000)> 16)
>>> B3 = chr (n & 0xff00)> 8)
>>> B4 = chr (n & 0xff)
>>> S = b1 + b2 + b3 + b4
>>> S
'\ X00 \ x9c @ C'
|
Very troublesome. If you replace it with a floating point number, you will be powerless.
Fortunately, Python provides a struct module to convert str and other binary data types.
The pack function of struct converts any data type into a string:
?
|
1
2
3 |
>>> Import struct
>>> Struct. pack ('> I', 10240099)
'\ X00 \ x9c @ C'
|
The first parameter of pack is the processing command. '> I' means:
> Indicates that the byte order is big-endian, that is, the network order. I indicates a 4-byte unsigned integer.
The number of subsequent parameters must be the same as that of the Processing Command.
Unpack converts str to the corresponding data type:
?
|
1
2 |
>>> Struct. unpack ('> IH', '\ xf0 \ xf0 \ xf0 \ xf0 \ x80 \ 8080 ')
(4042322160,328 96)
|
According to> IH instructions, the subsequent str values are converted to I: 4-byte unsigned integers and H: 2-byte unsigned integers.
Therefore, although Python is not suitable for writing code that operates byte streams at the underlying layer, it is much easier to use struct in areas with low performance requirements.
For the data types defined by the struct module, refer to the official Python documentation:
Https://docs.python.org/2/library/struct.html#format-characters
Windows map file (.bmp) is a very simple file format. Let's use struct to analyze it.
First, find a bmp file. If not, use "Drawing" to draw one.
Read the first 30 bytes for analysis:
?
|
1 |
>>> S = '\ x42 \ x4d \ x38 \ x8c \ x0a \ x00 \ x00 \ x00 \ x00 \ x00 \ x36 \ x00 \ x00 \ x00 \ x28 \ x00 \ x00 \ x00 \ x00 \ x80 \ x02 \ x00 \ x00 \ x68 \ x01 \ x00 \ x00 \ x01
\ X00 \ x18 \ x00'
|
The BMP format stores data in Small-end mode. The structure of the file header is as follows:
Two Bytes: 'bm 'indicates the Windows bitmap, and 'ba' indicates the OS/2 bitmap;
A 4-byte INTEGER: the bitmap size;
A 4-byte INTEGER: Reserved Bit, always 0;
A 4-byte INTEGER: the offset of the actual image;
A 4-byte INTEGER: the number of bytes of the Header;
A 4-byte INTEGER: The image width;
A 4-byte INTEGER: The image height;
A 2-byte INTEGER: Always 1;
A two-byte INTEGER: the number of colors.
Therefore, it can be combined to read with unpack:
?
|
1
2 |
>>> Struct. unpack ('<ccIIIIIIHH', s)
('B', 'M', 691256, 0, 54, 40,640,360, 1, 24)
|
The results show that 'B' and 'M' indicate Windows bitmap. The bitmap size is 640x360, and the color is 24.
Compile a BMP info. py file to check whether any file is a bitmap file. If yes, print the image size and color number.