Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the binary conversion module of python

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to understand the binary conversion module of python. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

In pyton, binary is converted by struct module, which mainly includes two kinds of functions, namely, pack for packaging and unpack for unpacking.

Among them, the input format of struct.pack is struct.pack (format, v1 Magi v2,...), where format is a format string, v1 Magi v2. Is the character that will be converted to bytes.

For example

> import struct > struct.pack ('iii', 15) b'\ X0f\ X00\ X00\ x00' > > struct.pack ('iii', 15,16,17) b'\ X0f\ X00\ X11\ X00\ x00 > > struct.pack ('breadth, 255) # b format values range from-128to 127struct.error: byte format requires-128> struct.pack (' breadth, 2005) b'\ xc8' > struct.pack ('B' B') 71) within # 128, you can use ASCII to denote baked G'.

I represents the binary code corresponding to int,15 F000, so its output is b\ x0f\ x00\ x00. When the format string is iii, it indicates that you want to convert three numbers to binary in the type of integer.

Each character in the formatted string represents a data type, in which the characters related to the integer type are as follows, the signed integer on the left and the unsigned integer on the right

C language type C language type standard size bsigned charBunsigned char1hshortHunsigned short2iintIunsigned int4llongLunsigned long4qlong longQunsigned long long8nssize_tNsize_t

Other types

Format C type Python type standard size x fill byte no byte string with cchar length 1? _ Boolbool1efloat2ffloatfloat4ddoublefloat8schar [] byte string pchar [] byte string Pvoid* integer

The function to get the standard size is provided in struct, calcsize:

> struct.calcsize ('B') 1 > struct.calcsize ('i') 4

In addition to declaring the data type when formatted, struct can also specify alignment, such as

> struct.pack ("I", 15) b'\ x0f\ x00\ x00\ x00' > > struct.pack ("> I", 15) # small end sequence b'\ x00\ x00\ x0f' > > struct.pack ("

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report