python struct.pack中的对齐字节问题
最近测试涉及到了序列字节化相关问题,碰到一个头疼的问题
buff = struct.pack("3s","B00")
print repr(buff)
输出:'B00'
buff = struct.pack('i',10172)
print repr(buff)
输出:"\xbc'\x00\x00"
buff = struct.pack("3si","B00",10172)
print repr(buff)
输出:"B00\x00\xbc'\x00\x00"
即:struct.pack("3s","B00")+struct.pack('i',10172) != struct.pack("3si","B00",10172)
但struct.pack("!3s","B00")+struct.pack('!i',10172) == struct.pack("!3si","B00",10172)是相等的
问题分析:
为了同c中的结构体交换数据,还要考虑有的c或c++编译器使用了字节对齐,通常是以4个字节为单位的32位系统,故而struct根据本地机器字节顺序转换.可以用格式中的第一个字符来改变对齐方式.定义如下:
| Character | Byte order | Size and alignment |
|---|---|---|
| @ | native | native 凑够4个字节 |
| = | native | standard 按原字节数 |
| < | little-endian | standard 按原字节数 |
| > | big-endian | standard 按原字节数 |
| ! | network (= big-endian) |
standard 按原字节数 |
使用方法是放在fmt的第一个位置,就像'@5s6sif'
参见如下
http://www.coder4.com/archives/3838
http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html
http://zhidao.baidu.com/link?url=Wd7SipMBMz7-lCTtnV3kUmjF4OMlRqZZUtfY0Zb3SqF5HKsCbwUBJFw8s2FfpTTv55Y-o-YHctDEfJoQ_ILTYAM8-sOWvOPS4aJtlHffNZ_
python struct.pack中的对齐字节问题的更多相关文章
- python struct.pack() 二进制文件,文件中打包二进制数据的存储与解析
学习Python的过程中,遇到一个问题,在<Python学习手册>(也就是<learning python>)中,元组.文件及其他章节里,关于处理二进制文件里,有这么一段代码的 ...
- python struct.pack方法报错argument for 's' must be a bytes object 解决
参考 https://blog.csdn.net/weixin_38383877/article/details/81100192 在python3下使用struct模块代码 fileHead = s ...
- python中struct.pack()函数和struct.unpack()函数
python中的struct主要是用来处理C结构数据的,读入时先转换为Python的字符串类型,然后再转换为Python的结构化类型,比如元组(tuple)啥的~.一般输入的渠道来源于文件或者网络的二 ...
- python struct中的pack unpack
python struct中的pack unpack pytyon tuple元组 print struct.unpack("!ihb", buffer) 结果为7 //pyth ...
- C语言中内存对齐规则讨论(struct)
C语言中内存对齐规则讨论(struct) 对齐: 现代计算机中内存空间都是按着byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地 ...
- 【转】在Python的struct模块中进行数据格式转换的方法
这篇文章主要介绍了在Python的struct模块中进行数据格式转换的方法,文中还给出了C语言和Python语言的数据类型比较,需要的朋友可以参考下 Python是一门非常简洁的语言,对于数据类型的表 ...
- C/C++中struct/union/class内存对齐
struct/union/class内存对齐原则有四个: 1).数据成员对齐规则:结构(struct)(或联合(union))的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员存储 ...
- C++中数据对齐问题。struct、union、enum,类继承。再谈sizeof()
首先是struct,在C++中,结构体其实和class有很大的相似了.但是有一点不同的是,struct默认是public,而class中是private. 当然,struct继承等用法也是可以的. 共 ...
- Python socket编程之二:【struct.pack】&【struct.unpack】
import struct """通过 socket 的 send 和 recv 只能传输 str 格式的数据""" "" ...
随机推荐
- Windows下载安装虚拟机和CentOS7
1,想要在Windows系统中安装linux系统,首先需要安装一个虚拟机VMware VMware 12下载地址:https://my.vmware.com/cn/web/vmware/info/sl ...
- 一段tcl代码
#!/usr/bin/wish proc icanspeak {} { set name [.ent get] } { exec s $name } } label .lab -text " ...
- bzoj 3681 Arietta
一棵有根树,每个点有一个音高,有 $m$ 中弹奏方法,每种方法可以弹奏 $d$ 子树中音高在 $[l,r]$ 间的音符,每种方法最多弹 $t$ 次 求最多能弹出多少个音符 $n \leq 10000$ ...
- String format方法的应用
String str=null; str=String.format("Hi,%s", "小超"); System.out.println(str); str= ...
- 【LeetCode】008. String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- Unity4.6 UGUI 图片打包设置(小图打包成图集 SpritePacker)
版权声明:本文转自http://blog.csdn.net/huutu 转载请带上 http://www.liveslives.com/ 在学习UGUI的过程中,一直使用小图也就是散图,一个按钮一个图 ...
- HDU1584(蜘蛛牌)
蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- scala 定时器
假如我们要开发一个定时器,该定时器每秒钟执行一定的动作,我们如何把要执行的动作传给定时器?最直观的回答是:传一个实现动作的函数(function) object Helloworld { def on ...
- linux中普通用户修改密码出现(passwd:Authentication token manipulation error)
如果在linux中,不管是root用户还是普通用户登录后,修改自己的密码,出现---passwd:Authentication token manipulation error---错误的解决办法: ...
- Windows_Server_2008远程桌面多用户登陆的配置方法
开启远程桌面后,Windows Vista(或Windows 2008)下默认只支持一个administrator用户登陆,一个登录后另一个就被踢掉了,下面提供允许同一个用户名同时多个用户登录的配置方 ...