python struct 总结

本文深入探讨了Python标准库中的struct模块,通过实例演示了如何使用struct进行数据打包和解包,包括不同字节顺序的处理,如大端和小端模式。通过对比char和byte在Python中的区别,帮助读者理解在二进制数据操作时的选择。

参考网址:

https://docs.python.org/3/library/struct.html
https://blog.youkuaiyun.com/wowocpp/article/details/79485868
https://blog.youkuaiyun.com/wowocpp/article/details/79487091

参考书籍:
Python标准库.[美]Doug Hellmann(详细书签).pdf
中文: 第二章 第6节

struct.pack('>BBHH', host, fc, 0, max_addr)

测试程序:

import array
import sys
import struct


# 80 04 01 00  00 10 ee 2b
data = b'\x80\x04\x01\x00\x00\x10\xee\x2b'

aa = data.encode('hex')

print aa

print aa.decode('hex')

print len(data)
bb = struct.unpack('BBBBBBBB', data)

print 'bb = ', bb

for dd in bb :
    print dd
    print type(dd)

a = 0x80
b = 0x04

c = struct.pack('ii',0x80,0x04)


print c

log:

800401000010ee2b
�  �+
8
bb =  (128, 4, 1, 0, 0, 16, 238, 43)
128
<type 'int'>
4
<type 'int'>
1
<type 'int'>
0
<type 'int'>
0
<type 'int'>
16
<type 'int'>
238
<type 'int'>
43
<type 'int'>
�      

注意 :python 中 char 和byte 的区别

(稍后补充)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值