python struct pack一个数组_Python中struct.pack的一个疑问

展开全部

的确很奇怪。我试验了10几分钟,你按下面的方法来做就可以正确。import struct

zzz = ("{Type:2}").encode("utf-8")

length = len(zzz)

a = struct.pack("%ds"%length,zzz)

print(a)

简单的说32313133353236313431303231363533e59b9ee7ad9431333335326138s其实只是一个字符,所以前面要加一个数字,比如8s,就可以存贮一个长度为8的字符串。你只写一个s当然只存贮一个。

python帮助的原文如下

For the 's' format

character, the count is interpreted as the size of the string, 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 string always has

exactly the specified number of bytes. As a special case, '0s' means a single, empty

string (while '0c'

means 0 characters).

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值