Methods of data format conversion in Python's struct module

Source: Internet
Author: User
Tags manual function prototype integer pack unpack in python

This article mainly introduces the method of data format conversion in Python's struct module, and gives the data type comparison between C language and Python language, which can be consulted by friends.

Python is a very concise language, for data type representations, unlike other languages that have predefined many types (such as: In C #, the optical integer defines 8), it defines only six basic types: strings, integers, floating-point numbers, tuples, lists, dictionaries. With these six types of data, we can do most of the work. But when Python needs to interact with other platforms over a network, it must take into account the problem of converting these data types to and from other platforms or languages. For example: C + + Write the client sent an int (4 bytes) variable data to the Python-written server, Python received a representation of the whole number of 4 bytes of data, how to parse into Python know the integer it? Python's standard module struct is used to solve this problem.

The content of the struct module is not too much, it is not too difficult, the following is the most common method to introduce:

Struct.pack

Struct.pack is used to convert a Python value to a string based on the format character (because there is no byte type in Python, the string here can be interpreted as a byte stream, or an array of bytes). Its function prototype is: Struct.pack (FMT, V1, v2, ...), parameter FMT is a format string. V1, v2, ... Represents the Python value to convert. The following example converts two integers to a string (a byte stream):

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 import struct A = b = m str = struct.pack ("II", A, b) #转换后的str虽然是字符串类型, but equivalent to a byte stream in another language (an array of bytes) that can transmit print ' length on the network : ', Len (str) Print str print repr (str) #----Result #length: 8 #----Here is garbled # '/x14/x00/x00/x00/x90/x01/x00/x00 '

The format character "I" represents the conversion to int, and ' II ' indicates that there are two int variables. After conversion, the result length is 8 bytes (int type occupies 4 bytes, two int is 8 bytes), you can see the result of the output is garbled, because the result is binary data, so the display is garbled. You can use Python's built-in function repr to get a recognizable string, where the 16-0x00000014, 0x00001009, respectively, represent 20 and 400.

Struct.unpack

Struct.unpack does the work just as opposed to struct.pack, which is used to swap byte flows for Python data types. Its function prototype is: Struct.unpack (FMT, String), which returns a tuple. The following is a simple example:

?

1 2 3 4 5 6 7 8 9 str = struct.pack ("II", A1), a2 = Struct.unpack ("II", str) print ' A1: ', A1 print ' A2: ', A2 #----Result: #a1:20 #a2: Struct.calcsize

Struct.calcsize is used to calculate the length of the result of a format string, such as: Struct.calcsize (' II '), which returns 8. Because two int types occupy a length of 8 bytes.

Struct.pack_into, Struct.unpack_from

These two functions are described in the Python manual, but do not give examples of how to use them. In fact, they are not used in many practical applications. Google for a long time, only to find an example, posted out to share:

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 Import struct from ctypes import create_string_buffer buf = Create_string_buffer (a) print repr (Buf.raw) struct.pack_i Nto ("III", BUF, 0, 1, 2,-1) Print repr (buf.raw) Print struct.unpack_from (' III ', BUF, 0) #----Result # '/x00/x00/x00/x 00/x00/x00/x00/x00/x00/x00/x00/x00 ' # '/x01/x00/x00/x00/x02/x00/x00/x00/xff/xff/xff/xff ' # (1, 2,-1)

About Format Strings

In the Python manual, a format character that corresponds to the Python type is given for the common type in C language:

Please refer to the Python manual struct module for specific content

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.