python 二进制处理

在使用python进行二进制数据处理的时候,需要用到pack和unpack。

在pack/unpack 的参数中,除了表示格式的字母或符号,还会出现数字,通过查阅python的官方文档,记录如下

A format character may be preceded by an integral repeat count. For example, the format string '4h' means exactly the same as 'hhhh'. 字母前的数字表示重复该字母多少次

Whitespace characters between formats are ignored; a count and its format must not contain whitespace though.

For the 's' format character, the count is interpreted as the length of the bytes, not a repeat count like for the other format characters; for example, '10s' means a single 10-byte string, while '10c' means 10 characters. If a count is not given, it defaults to 1. For packing, the string is truncated or padded with null bytes as appropriate to make it fit. For unpacking, the resulting bytes object always has exactly the specified number of bytes. As a special case, '0s' means a single, empty string (while '0c' means 0 characters). 对于字符串string 和字符char,前面的数字是由差别的。string前的数字表明该string的长度。而char前的数字表示重复char多少次。

详情请参考 python 官方文档https://docs.python.org/3/library/struct.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值