加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python字节缓冲区对象?

发布时间:2020-12-16 23:37:00 所属栏目:Python 来源:网络整理
导读:在 Python中有一个字节缓冲区对象,我可以附加特定类型的值吗? (优选具有可指定的端性) 例如: buf.add_int(4) # should add a 4 byte integerbuf.add_short(10) # should add a 2 byte shortbuf.add_byte(24) # should add a byte 我知道我可以使用struct.p
在 Python中有一个字节缓冲区对象,我可以附加特定类型的值吗? (优选具有可指定的端性)

例如:

buf.add_int(4)    # should add a 4 byte integer
buf.add_short(10) # should add a 2 byte short
buf.add_byte(24)  # should add a byte

我知道我可以使用struct.pack,但这种方法看起来更容易.理想情况下,它应该像Java中的DataOutputStream和DataInputStream对象一样完成任务.

解决方法

你可以随时使用 bitstring.它能够做所有你要求的事情和更多.
>>> import bitstring
>>> stream=bitstring.BitStream()
>>> stream.append("int:32=4")
>>> stream.append("int:16=10")
>>> stream.append("int:8=24")
>>> stream
BitStream('0x00000004000a18')
>>> stream.bytes
'x00x00x00x04x00nx18'

这是documentation的链接.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读