学物联网,来万物简单IoT物联网!! 
 
  
ustruct提供打包和解压原始数据类型的功能。
 
字符 自己额顺序 大小 对其方式 @ native native native = native standard none < little-endian standard none > big-endian standard none ! network (= big-endian) standard none 
 
格式化字符表 
 
格式 C语言的类型 Python对应的类型 类型所占空间大小 x pad byte no value c char bytes 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 unsigned int integer 4 l long integer 4 L unsigned long integer 4 q long long integer 8 Q unsigned long long integer 8 n ssize_t integer N size_t integer f float float 4 d double float 8 s char[] bytes p char[] bytes P void * integer 
 
函数原型:ustruct.calcsize(fmt)  参数说明: 参数 类型 必选参数? 说明 fmt 格式字符 是 详情参考上文格化式字符表 
 
返回值 示例: >> >  ustruct. calcsize( 'i' )  4  
>> >  ustruct. calcsize( 'd' )  8 
函数原型:ustruct.pack(fmt, v1, v2, …)  参数说明: 参数 类型 必选参数? 说明 fmt string 是 压缩参数时所采用的格式字串,详情见上文格化式字符表 v1 - 是 要压缩的对象v1 v2 - 是 要压缩的对象v2 … - 是 要压缩的其它对象 
 
 
>> >  ustruct. pack( 'iiiii' ,  1 ,  2 ,  3 ,  4 ,  5 )  
b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x 00\x00' 
函数原型:unstrcut.unpack(fmt, data)  参数说明: 参数 类型 必选参数? 说明 fmt string 是 解压缩数据时所采用的格式字串,详情见上文格化式字符表 data bytearray 是 要解压缩的对象v1 
 
 
>> >  ustruct. unpack( 'iiiii' ,  b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x 00\x04\x00\x00\x00\x05\x00\x00\x00' ) 
( 1 ,  2 ,  3 ,  4 ,  5 )