struct MODULE
#A struct module is used to convert a numeric string into a fixed-length byte.#format:#X:pad byte (no data); c:char; b:signed byte; b:unsigned byte;#?: _bool (requires C99; if not available, char is used instead)#H:short; h:unsigned Short; I:int; i:unsigned int;#L:long; L:unsigned long; F:float; D:double.#Special Cases (preceding decimal count indicates length):#s:string (array of char); P:pascal string (with Count byte).#Special cases (only available in native format):#n:ssize_t; n:size_t;#P:an integer type is wide enough to hold a pointer.#Special case (not in native mode unless ' long long ' in platform C):#Q:long Long; q:unsigned Long LongImportstruct#a = struct.pack (' i ', 4658) # ' I ' mode converted to 4 bytes#Print (A,len (a)) # B ' 2\x12\x00\x00 ' 4#B = struct.unpack (' i ', a)#print (b) # (4658,)#print (b[0]) # Unpack after the data is a tuple#a = Struct.pack (' f ', 5641564987)#Print (A,len (a)) # B ' 2\x12\x00\x00 ' 4#B = Struct.unpack (' f ', a)#print (b) # (4658,)#print (b[0]) # Unpack after the data is a tuple## Output## b ' \xba!\xa8o ' 4## (5641565184.0,)## 5641565184.0
Python Road--struct Module