Python中用struct模块处理二进制数据

发布时间:2023-03-12 09:17:04   来源:文档文库   
字号:

Python中用struct模块处理二进制数据
有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用pythonstruct模块来完成.可以用struct来处理c语言中的结构体.struct模块中最重要的三个函数是pack(,unpack(,calcsize(pack(fmt,v1,v2,...按照给定的格式(fmt,把数据封装成字符串(实际上是类似c结构体的字节流
unpack(fmt,string按照给定的格式(fmt解析字节流string返回解析出来的tuplecalcsize(fmt计算给定的格式(fmt占用多少字节的内存
struct中支持的格式如下表:
>格式c类型python类型
>xchar(表示填充字节
>cchar长度为1的字符串
>bsignedcharinteger>Bunsignedchar
integer

>hshort
integer>Hunsignedshortinteger>iint
integer>Iunsignedint
long>llong
integer>Lunsignedlonglong>qlonglong
long>Qunsignedlonglong>ffloat
float>ddouble
float>schar[]
string>pchar[]
string>Pvoid*integerlong

本文来源:https://www.2haoxitong.net/k/doc/6726f20a52ea551810a68738.html

《Python中用struct模块处理二进制数据.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式