Python built-in module struct instance details, pythonstruct
This article focuses on the relevant content of the Python built-in module struct, as follows.
In Python, the types of variables are only list, ancestor, Dictionary, set, and other advanced abstract types. The underlying primary types such as bit, byte, and integer are not defined in c. Because Python is a high-level explanatory language, it runs at the underlying layer after being translated. How to break through the type definition barriers between Python and other languages, the Python built-in module struct completely solves all problems.
Knowledge introduction:
Three of the most commonly used struct modules:
(1) struct. pack: it is used to convert Python values to strings Based on format characters (because Python does not have the Byte type, the strings here can be understood as Byte streams or Byte arrays ).
(2) struct. unpack: opposite to struct. pack. It is used to convert byte Transfer to python data type. This function returns a tuples.
(3) struct. calcsize: Calculate the length of the result corresponding to the format string.
Format operations encountered during conversion:
| Format character |
C Language |
Python type |
| X |
Pad byte |
No value |
| C |
Char |
String of length 1 |
| B |
Signed char |
Integer |
| B |
Unsigned char |
Integer |
| ? |
_ Bool |
Bool |
| H |
Short |
Integer |
| H |
Unsigned short |
Integer |
| I |
Int |
Integer |
| I |
Unsigned int |
Integer or long |
| L |
Long |
Integer |
| L |
Unsigned long |
Long |
| Q |
Long |
Long |
| Q |
Unsigned long |
Long |
| F |
Float |
Float |
| D |
Double |
Float |
| S |
Char [] |
String |
| P |
Char [] |
String |
| P |
Void * |
Long |
Instance details:
#! /Usr/bin/python #-*-coding: UTF-8-*-''' test struct module ''' from struct import * import arraydef fun_calcsize (): print 'Ci: ', calcsize ('ci') # memory size occupied by calculation formats print' @ ci: ', calcsize (' @ ci') print '= ci :', calcsize ('= ci') print'> ci: ', calcsize ('> ci') print '<ci:', calcsize ('<ci') print 'ic: ', calcsize ('ic') # memory size occupied by calculation formats print '@ ic:', calcsize ('@ ic') print '= ic :', calcsize ('= IC') print'> ic: ', calcsize ('> IC') print '<ic:', calcsi Ze ('<ic') def fun_pack (Format, msg = [0x11223344,0x55667788]): result = pack (Format, * msg) print 'package '. ljust (10), str (type (result )). ljust (20), for I in result: print hex (ord (I), # ord converts the characters in the ASCII code table to the corresponding integer, hex converts the value to hexadecimal print result = unpack (Format, result) print 'unpackage '. ljust (10), str (type (result )). ljust (20), for I in result: print hex (I), print def fun_pack_into (Format, msg = [0x11223344,0x55667788]): R = array. array ('C', ''' * 8) # Variable buffer with a size of 8, writable buffer result = pack_into (Format, r, 0, * msg) print 'pack _ '. ljust (10), str (type (result )). ljust (20), for I in r. tostring (): print hex (ord (I), print result = unpack_from (Format, r, 0) print 'pack _ from '. ljust (10), str (type (result )). ljust (20), for I in result: print hex (I), printdef IsBig_Endian (): ''' determines whether the machine is large or small. ''' a = 0x12345678 result = pack ('I', a) # resu Lt is a string, and the string is stored in bytes in the same binary format as that of. If hex (ord (result [0]) = '0x78': print 'the local machine is a small-end 'else: print' the local machine is a large-end 'def test (): a = '000000' for I in a: print 'binary character % s:' % I, hex (ord (I )) # The character corresponds to the hexadecimal ''' of the corresponding integer in the ascii code table. You can also use the pack () function without the data returned by unpack (). As long as the unpack string conforms to the unpack format, pack () will re-interpret the binary character string in the memory according to the decommission format (it doesn't feel good ..., as shown in the following example) ''' print 'Big end: ', hex (unpack ('> I ', a) [0]) # Because pack returns a tuples, even if only one element is in the form of a tuples, print 'small ends: ', hex (unpack (' <I ', a) [0]) if _ name _ = "_ main _": print 'determines whether the local machine is large or not. End? ', IsBig_Endian () fun_calcsize () print' big end: 'format = "> ii" fun_pack (Format) fun_pack_into (Format) print 'small end: 'format = "<ii" fun_pack (Format) fun_pack_into (Format) print 'test' test () ''' result: determines whether the local machine is large or small? The local machine is a small-end ci: 8 @ ci: 8 = ci: 5> ci: 5 <ci: 5 ic: 5 @ ic: 5 = ic: 5> ic: 5 <ic: 5 major ends: pack <type 'str'> 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 unpack <type 'tuple'> 0x11223344 0x55667788 pack_into <type 'nonetype '> 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 pack_from <type 'tuple '> 0x11223344 0x55667788 small ends: pack <type 'str'> 0x44 0x33 0x22 0x11 0x88 0x77 0x66 0x55 unpack <type 'tuple'> 0x11223344 0x55667788 pack_into <type 'nonetype '> 0x44 0x33 0x22 0x11 0x88 0x77 0x66 0x55 pack_from <type 'tuple '> 0x11223344 0x55667788 test character 1 binary: 0x31 character 2 binary: 0x32 character 3 binary: 0x33 character 4 binary: 0x34 big end: 0x31323334 Small End: 0x34333231 '''
Python software environment used in this example: win10 + anaconda3 + pycharm, Python version: 3.6
Summary
The above is all the details about the Python built-in module struct instance, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!