Usage of struct in python

Source: Internet
Author: User

 

The following describes several methods in this module.

Struct. pack (): In my understanding, python uses the struct module to split characters (such as int, long, And unsized int) into byte streams (expressed in hexadecimal notation) for easy transmission.

The function prototype is struct. pack (fmt, v1, v2 ,...), fmt indicates the type, followed by V1 v2 .. if the number of parameters is exceeded, the number of corresponding types is exceeded.

>>> Import struct >>> x = 10 >>> y = 20 >>> str = struct. pack ('II', x, y) >>> print str >>>>> print repr (str) '\ n \ x00 \ x00 \ x00 \ x14 \ x00 \ x00 \ x00' # small terminal Method

It should be noted that the sub-terminal method indicates that the maximum valid byte is at the beginning. For more information, see P26.

In hexadecimal notation, the value range of one byte is 00 ~ FF.

 

Struct. unpack (): I understand that the split byte stream is reassembled into int type.

>>> X1, y1 = struct. unpack ('II', str) >>> print 'x1: ', x1x1: 10 >>> print 'y1:', y1y1: 20 >>>

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.