Home Backend Development Python Tutorial Detailed explanation of Python's method of using struct to process binary data

Detailed explanation of Python's method of using struct to process binary data

Sep 13, 2017 am 10:05 AM
python struct binary

This article mainly introduces relevant information about Python's use of struct to process binary examples. I hope that everyone can master this part of the content through this article. Friends in need can refer to it

Python uses struct to process Detailed explanation of binary examples

Sometimes you need to use python to process binary data, for example, when accessing files and socket operations. At this time, you can use python's struct module to complete it. You can use struct to process it. Structure in c language.

  • The three most important functions in the struct module are pack(), unpack(), calcsize()

  • pack(fmt, v1, v2, ...) Pack the data into a string according to the given format (fmt) (actually a byte stream similar to a c structure)

  • unpack(fmt, string) Parses the byte stream string according to the given format (fmt) and returns the parsed tuple

  • calcsize(fmt) Calculation How many bytes of memory does a given format (fmt) occupy?

The supported formats in struct are as follows:

##Qunsigned long longlong8 ffloatfloat4##dsp##Pvoid *longNote 1.q and Q are only interesting when the machine supports 64-bit operationNote 2.Every There can be a number before each format, indicating the number
Format C Type Python Number of bytes
x pad byte no value 1
c char string of length 1 1
b signed char integer 1
B unsigned char integer 1
? _Bool bool 1
h short integer 2
H unsigned short integer 2
i int integer 4
I unsigned int integer or long 4
l long integer 4
L unsigned long long 4
q long long long 8
double float 8
char[] string 1
char[] string 1

Note 3. The s format represents a string of a certain length, 4s represents a string of length 4, but p represents a pascal string

Note 4.P Used to convert a pointer, its length is related to the machine word length
Note 5. The last one can be used to represent the pointer type, occupying 4 bytes


In order to be the same as the structure in c When exchanging data, you should also consider that some c or c++ compilers use byte alignment, usually in 32-bit systems with 4 bytes, so the struct is converted according to the local machine byte order. You can use the first character in the format characters to change the alignment. The definition is as follows:


##CharacterByte order@nativenativelittle-endian##>big-endianstandard Based on the original number of bytes##! standard Based on the original number of bytes

The method of use is to put it in the first position of fmt, like '@5s6sif'

Example 1:

For example A structure


struct Header
{
  unsigned short id;
  char[4] tag;
  unsigned int version;
  unsigned int count;
}
Copy after login

received the above structure data through socket.recv, which is stored in the string s. Now it needs to be parsed out. You can use unpack() function.


import struct id, tag, version, count = struct.unpack("!H4s2I", s)
Copy after login

In the above format string, ! indicates that we want to use network byte order parsing, because our data is from the network When received, it is in network byte order when transmitted on the network. The following H represents an unsigned short id, 4s represents a 4-byte long string, and 2I represents two unsigned int type data.

Just through an unpack, now our information has been saved in id, tag, version, and count.

Similarly, it is also very convenient to pack local data into a struct Format.


ss = struct.pack("!H4s2I", id, tag, version, count);
Copy after login

The pack function converts id, tag, version, count into the structure Header according to the specified format. ss is now a string ( It is actually a byte stream similar to a c structure), and this string can be sent out through socket.send(ss).

Example 2:


import struct
a=12.34
#将a变为二进制
bytes=struct.pack('i',a)
Copy after login

At this time bytes is a string string, and the string is the same as the binary storage content of a in bytes.

Then reverse the operation

Existing binary data bytes, (actually a string), convert it in turn into the python data type:


a,=struct.unpack('i',bytes)
Copy after login

Note that unpack returns a tuple

So if there is only one variable:


bytes=struct.pack('i',a)
Copy after login

Then, you need to do this when decoding


a,=struct.unpack('i',bytes) 或者 (a,)=struct.unpack('i',bytes)
Copy after login

If you use a=struct.unpack('i',bytes) directly, then a=( 12.34,) is a tuple instead of the original floating point number.

If it is composed of multiple data, it can be like this:


a='hello'
b='world!'
c=2
d=45.123
bytes=struct.pack('5s6sif',a,b,c,d)
Copy after login

The bytes at this time are data in binary form, and you can directly Write a file such as binfile.write(bytes)

Then, when we need it, we can read it out, bytes=binfile.read()

and then pass struct. unpack() is decoded into a python variable


a,b,c,d=struct.unpack('5s6sif',bytes)
Copy after login

'5s6sif' is called fmt, which is a format string, consisting of numbers and characters, 5s means 5 A string of characters, 2i, represents 2 integers, etc. The following are the available characters and types. The ctype representation can correspond to the types in Python one-to-one.

Note: Problems encountered when processing binary files

When we process binary files, we need to use the following methods


binfile=open(filepath,'rb')  读二进制文件

binfile=open(filepath,'wb')  写二进制文件
Copy after login

So what is the difference from the result of binfile=open(filepath,'r')?

There are two differences:

First, if you encounter '0x1A' when using 'r', it will be regarded as the end of the file, which is EOF. Using 'rb' does not have this problem. That is, if you write in binary and read out in text, only part of the file will be read out if '0X1A' is present. When using 'rb', it will read to the end of the file.

Second, for the string x='abc\ndef', we can use len(x) to get its length to be 7, \n we call it a newline character, which is actually '0X0A'. When we write in 'w', which is text mode, '0X0A' will be automatically changed into two characters '0X0D', '0X0A' on the Windows platform, that is, the file length actually becomes 8. When reading in 'r' text mode, it is automatically converted to the original newline character. If you change to 'wb' binary mode to write, one character will remain unchanged, and it will be read as it is when reading. So if you write in text mode and read in binary mode, you have to consider this extra byte. '0X0D' is also called the carriage return character. It will not change under Linux. Because linux only uses '0X0A' to represent line breaks.

Size and alignment
native Make up enough 4 bytes =
standard                                                                                                                            can be based on the original number of bytes <
standard                  can be canned according to the original number of bytes Number of sections
network (= big-endian)

The above is the detailed content of Detailed explanation of Python's method of using struct to process binary data. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

See all articles