Python struct Library

Source: Internet
Author: User

The socket library of ython uses the string type to send and receive data, so that when we use

I = socket. Recv (4)

To receive a 4-byte integer, the integer is actually saved in binary form in the string I
In most cases, we need a real integer/long type instead of an integer in the string type. In this case, we can use struct
Library: interpret strings as packed binary data. For the above situation, we can write

T = unpack ("I", I)

The first parameter is a formatted string. I indicates that the first data item contained in string I is an integer in the C language unsigned integer. Here I
Only one data item is included. In fact, the interpreted string can contain multiple data items. You only need to specify a format for each data item in the formatted string. Naturally, unpack returns
A tuple type.

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.