Python Learning note __12.4 struct

Source: Internet
Author: User

# This is a learning note for the Liaoche teacher Python tutorial

1 , overview

struct modules are used to resolve conversions for bytes and other binary data types

1) struct of the Pack function to turn any data type into bytes :

>>> Import struct

>>> struct. Pack (' >i ', 10240099)

B ' \x00\[email protected] '

Parameters for Pack:

The first parameter is a processing instruction:

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

The second parameter is the passed in parameter:

The number of arguments passed in should be the same as the processing instructions.

2) Unpack put bytes into the corresponding data type:

>>> struct. Unpack (' >ih ', b ' \xf0\xf0\xf0\xf0\x80\x80 ')

(4042322160, 32896)

According to >ih 's instructions, the following bytes turn into:

I: 4-byte unsigned integers and H: 2-byte unsigned integers.

3) BMP of the file Unpack

# Use Paint to draw a BMP file and read the first 30 bytes for analysis

>>> 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 '

# Read with unpack

>>> struct.unpack (' <cciiiiiihh ', s) # < indicates that the byte order is the host byte-order Little endian

(b ' B ', B ' M ', 691256, 0, 54, 40, 640, 360, 1, 24)

Two bytes: ' BM ' represents the Windows Bitmap, ' BA ' represents the OS/2 bitmap;

A 4-byte integer: Represents 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 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.

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

4) Reference Documentation

the data type defined by the struct module (https://docs.python.org/3/library/struct.html#format-characters)

Python network byte order and host byte order (https://www.linuxidc.com/Linux/2014-02/97157.htm)

2 , examples

1 , write a bmpinfo.py, you can check whether any file is a bitmap file, if so, print out the image size and number of colors

#-*-Coding:utf-8-*-

Import base64, struct

Bmp_data = Base64.b64decode (' qk1oagaaaaaaadyaaaaoaaaahaaaaaoaaaababaaaaaaadicaaascwaaegsaaaaaaaaaaaaa/3//f/9//3/ /f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9/ahwafab8ahwafab8ahwafp9//3//fwb8ahwafab8/ 3//f/9/ahwafab8ahz/f/9//3//f/9//38afab8ahwafab8ahwafab8ahz/f/9//38afab8/3//f/9//3//fwb8ahz/f/9//3//f/9//3//f/9 /ahwafp9//3//f/9/ahwafp9//3//fwb8ahz/f/9//3//f/9/ahwafp9//3//f/9//3//f/9//38afab8ahwafab8ahwafp9//3//f/9/ ahwafp9//3//f/9//38afab8/3//f/9//3//f/9//3//fwb8ahwafab8ahwafab8/3//f/9//38afab8/3//f/9//3//fwb8ahz/f/9//3//f/ 9//3//f/9/ahwafp9//3//f/9/ahwafp9//3//fwb8ahz/f/9/ahz/f/9/ahwafp9//38afp9//3//f/9/ahwafab8ahwafab8ahwafab8/3// f/9/ahwafp9//38afab8ahwafab8ahwafab8/3//f/9//38afab8ahwafab8ahwafab8/3//f/9/ahwafab8ahz/ fwb8ahwafab8ahwafab8ahz/f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//38aaa== ')

def bmp_info (data):

str=struct.unpack (' <cciiiiiihh ', data[:30]) #bytes class also has a slicing method

If str[0]==b ' B ' and Str[1]==b ' M ':

Print ("This is a bitmap file")

return {

' Width ': str[-4],

' Height ': str[-3],

' Color ': str[-1]

}

Else

Print ("This is not a bitmap file")

# test

Bi = bmp_info (bmp_data)

Assert bi[' width '] = = 28

Assert bi[' height '] = = 10

Assert bi[' color '] = = 16

Print (' OK ')


Python Learning note __12.4 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.