Python struct MODULE

Source: Internet
Author: User

      • Python struct MODULE
        • Use
        • Functions in a struct module
        • formatting strings
          • Alignment
          • Format character
        • Code usage examples
        • Python reference manual struct module links

    Reprinted (http://blog.csdn.net/lis_12/article/details/52777983).

    Use
      1. Converts the Python data to a string in the specified format, which is a byte stream, such as when the network is transmitted, the int cannot be transferred, and then the int is converted into a stream of bytes before being sent;
      2. Converts a byte stream to the data type specified by Python in the specified format;
      3. Processing binary data, if using a struct to process the file, you need to use ' WB ', ' RB ' to write in binary (byte stream), read the way to deal with the file;
      4. To deal with the structure in C language;
    Functions in a struct module
    function return explain
    Pack (Fmt,v1,v2 ...) String Converts the data to a string (byte stream) in the given format (FMT) and returns the string.
    Pack_into (Fmt,buffer,offset,v1,v2 ...) None Converts the data to a string (byte stream) in the given format (FMT), and writes the byte stream to buffer starting with offset. (buffer is writable and can be used with the array module)
    Unpack (Fmt,v1,v2 ...) Tuple Parses a byte stream in the given format (FMT) and returns the parse result
    Pack_from (Fmt,buffer,offset) Tuple Resolves a buffer starting with offset in the given format (FMT) and returns the parse result
    CalcSize (FMT) Size of FMT Calculates how many bytes of memory are occupied by the given format (FMT), and notes the alignment
    formatting strings

    When packaging or unpacking, you need to package or unpack in a specific way. This is the format string, which specifies the data type, in addition to the special characters that control the order, size, and alignment of the byte.

    Alignment

    In order to exchange data with structs in C, it is also considered that the C or C + + compiler uses byte alignment, usually a 32-bit system in 4 bytes, and therefore the struct is converted according to the local machine byte order. You can change the alignment by using the first character in the format. defined as follows

    Character Byte Order Size Alignment
    @ (default) Natively Natively This machine, gather enough 4 bytes
    = Natively Standard None, according to the original number of bytes
    < Small end Standard None, according to the original number of bytes
    > Big End Standard None, according to the original number of bytes
    ! Network (big-endian) Standard None, according to the original number of bytes

    If you do not understand the size of the end, see the size end reference URL.

    Format character Standard
    format character C language Type python typesize
    X Pad byte (padding bytes) No value
    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 (Uppercase i) unsigned int Integer 4
    L (lowercase L) Long Integer 4
    L unsigned long Long 4
    Q Long Long Long 8
    Q unsigned long long Long 8
    F Float Float 4
    D Double Float 8
    S Char[] String
    P Char[] String
    P void * Long

    NOTE--!

      1. _bool is defined in C99, and if there is no such type, the type is treated as char, a byte;
      2. Q and q are only available for 64-bit machines;
      3. Each format can have a number, indicating the number of this type, such as the S format for a certain length of string, 4s for a length of 4 strings, 4i for four int;
      4. P is used to convert a pointer whose length is related to the computer;
      5. The length of F and D is related to the computer;

Python struct MODULE

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.