Python Learning notes (35) struct

Source: Internet
Author: User
Tags processing instruction unpack

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.

structfunction to turn any data type into bytes : pack

Import struct>>> struct.pack ('>i', 10240099) b'  \x00\[email protected]'

packThe 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.

unpackTurn 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 .

structThe 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

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.