Python module -- struct (byte stream, package splitting implementation)

Source: Internet
Author: User

   We know that python only defines six data types: String, integer, floating point, list, tuples, and dictionary. But how can I implement some byte variables in C language in python? This is important, especially for data transmission over the network.

   Python provides a struct module to provide conversion. The following describes several methods in this module.

   Struct. pack ():

   Struct. pack is used to convert Python values to strings Based on format characters (because Python does not have the Byte type, you can understand the strings here as Byte streams or Byte arrays ). The function prototype is struct. pack (fmt, v1, v2,...). The fmt parameter is a format string. The following describes the information about the format string. V1, v2,... indicates the python value to be converted. The following example converts two integers to a string (byte stream ):

>>> Import struct
>>> A = 20
>>> B = 400
>>> Str = struct. pack ('II', a, B)  # Convert to word throttling. Although it is still a string, it can be transmitted over the network.
>>> Print len (str)             # Ii indicates two int
8                              # We can see that the length is 8 bytes, which is exactly the length of two int-type data.
>>> Print Str
                              # Binary is garbled
>>> Print repr (STR)
'\ X14 \ x00 \ x00 \ x00 \ x90 \ x01 \ x00 \ x00' # The hexadecimal values 0x00000014 and 0x00001009 indicate 20 and 400 respectively.
>>>

  Then we can perform any group package. For example, the following package example only describes the pack

 

Format = "! HH % ds "% len (data)
Buffer = struct. pack (format, opcode, blocknumber, data)

 We need to package a data and add some headers. Based on the following format characters, we know that H is an unsigned short of 2 bytes, and s is a char type. So this buffer is two bytes of opcode, two bytes of blocknumber, And len long char.

 

  Struct. unpack ():

  We then run the following example:

>>> A1, a2 = struct. unpack ('II', str)
>>> Print 'a1', a1
A1 20
>>> Print 'a2 = ', a2
A2 = 400.

We can see that "ii" divides the eight-byte str into two int integers based on four bytes.

 Struct. calcsize (): used to calculate the output size of a specific format, which is several bytes, such:

>>> Struct. calcsize ('hh4s ')
8
>>> Struct. calcsize ('II ')
8
>>>

>>> Format = '! HH % ds '% len ('Hello python ')
>>> Struct. calcsize (format)
16
>>>

 

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.