Python's Common module (struct) goes

Source: Internet
Author: User

To be precise, Python does not have a data type that specifically handles bytes. But because it is both a string and a str byte, the byte array is =str. In the C language, we can easily use structs, unions to handle bytes, and the conversion of bytes and int,float.

In Python, for example, to turn a 32-bit unsigned integer into a byte, or 4-length str, you have to do this with the bitwise operator:

>>> 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\[email protected]‘

Very troublesome. If you replace it with a floating point, you can do nothing.

Fortunately, Python provides a struct module to resolve str conversions to and from other binary data types.

structpackfunction to turn any data type into a string:

>>> import struct>>> struct.pack(‘>I‘, 10240099)‘\x00\[email protected]‘

packThe first parameter is the processing instruction, ‘>I‘ which means:

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

The number of parameters to be followed is the same as the processing instructions.

unpackTurn it str into the appropriate data type:

>>> struct.unpack(‘>IH‘, ‘\xf0\xf0\xf0\xf0\x80\x80‘)(4042322160, 32896)

Depending on >IH the description, the str following changes to I : 4-byte unsigned integer and H : 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 .

structThe data types defined by the module can refer to the official Python documentation:

Https://docs.python.org/2/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 = ‘\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 actual image offset; 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‘, ‘M‘, 691256, 0, 54, 40, 640, 360, 1, 24)

The results are displayed, the ‘B‘ ‘M‘ description is a Windows bitmap, the bitmap size is 640x360, and the number of colors is 24.

Transfer from Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 0013994173393204e80af1f8fa94c8e9d094d229395ea43000

Python's Common module (struct) goes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.