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 >>>