Introduction to the struct module in Python

Source: Internet
Author: User

This article mainly introduces the struct module in Python, the code is based on the python2.x version, the need for friends can refer to the

To be precise, Python does not specifically deal with byte data types. However, since STR is both a string and can represent bytes, the byte array is =str. In the C language, we can easily use struct, union to process bytes, and byte and Int,float conversions.

In Python, for example, to turn a 32-bit unsigned integer into a byte, that is, 4-length str, you have to work with the bitwise operator to write:

?

 1 2 3 4 5 6 7 8 >>> n = 10240099 >>> B1 = Chr ((N & 0xff000000) >>) >>> b2 = Chr ((N & 0xff0000) >>) >>> B3 = Chr ((N & 0xff00) >> 8) >>> b4 = Chr (n & 0xff) >>> s = b1 + B2 + B3 + b4 >>> s ' x00x9c@c '

Very troublesome. There's nothing you can do if you change to a floating-point number.

The good news is that Python provides a struct module to address the conversion of STR and other binary data types.

The struct pack function turns any data type into a string:

?

 1 2 3 >>> import struct >>> struct.pack (' >i ', 10240099) ' X00x9c@c '

The first parameter of the pack is the processing instruction, and ' >i ' means:

> indicates that the byte order is Big-endian, which is the network order, and I represents a 4-byte unsigned integer.

The number of parameters to follow is consistent with the processing instructions.

Unpack turns str into the appropriate data type:

?

 1 2 >>> struct.unpack (' >ih ', ' xf0xf0xf0xf0x80x80 ') (4042322160, 32896)

According to >IH, the following str turns into a i:4 byte unsigned integer and an H: 2-byte unsigned integer.

So, while Python is not suitable for coding the underlying operation Byte stream, it is much more convenient to use struct where performance requirements are low.

The data types defined by the struct module can be referenced in the official Python documentation:

Https://docs.python.org/2/library/struct.html#format-characters

The Windows bitmap file (. bmp) is a very simple file format that we can analyze with struct.

First find a BMP file, no words with "paint" draw a.

Read the first 30 bytes to parse:

?

 1 >>> s = ' x42x4dx38x8cx0ax00x00x00x00x00x36x00x00x00x28x00x00x00x80x02x00x00x68x01x00x00x01 x00x18x00 '

BMP format to store data in a small way, the structure of the file header in order as follows:

Two bytes: ' BM ' means Windows Bitmap, ' BA ' denotes os/2 bitmap;

A 4-byte integer that represents the bitmap size;

A 4-byte integer: A reserved bit, always 0;

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 to read with unpack:

?

 1 2 >>> struct.unpack ('

The results show that the ' B ', ' M ' description is a Windows bitmap, the bitmap size is 640x360, and the number of colors is 24.

Write a bmpinfo.py to check if any file is a bitmap file, and if so, print out the size and number of colors.

Related Keywords:
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD