struct.pack()、struct.unpack()和struct.calcsize()

python处理二进制字节流时,或跨平台如对标c/c++的short、long、double、结构体等类型,或处理socket数据、文件等情况,可以用struct,转换和解析函数为struct.pack()和struct.unpack()。

help(struct.pack)
           
Help on built-in function pack in module _struct:

pack(...)
    pack(fmt, v1, v2, ...) -> bytes  按照给定的格式(fmt),把数据封装成字符串(实际上是类似于c结构体的字节流)。
    
    Return a bytes object containing the values v1, v2, ... packed according
    to the format string fmt.  See help(struct) for more on format strings.

>>> help(struct.unpack)
           
Help on built-in function unpack in module _struct:

unpack(...)
    unpack(fmt, buffer) -> (v1, v2, ...)  按照给定的格式(fmt)解析字节流string,返回解析出来的tuple。
    
    Return a tuple containing values unpacked according to the format string
    fmt.  The buffer's size in bytes must be calcsize(fmt). See help(struct)
    for more on format strings.<

  • 6
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值