[Python]_[初级]_[使用struct库对二进制文件进行读写]

场景

1.要分析或生成一个二进制文件时, Python使用 struct库来转换或分析二进制数据. 当然使用C++或者Java来分析也可以, 但对于脚本语言Python来说, 编写数据和调试时间都没Python快. 所以Python其实是比较正确的选择.

2.如果需要传输socket数据, 在定义好数据格式时自然也是使用stuct.pack和unpack来打包数据和分析数据.

说明

1.Python作为一门通用型脚本语言, 能像Java,C++那样处理通用型任务,比如读写二进制或文本文件. 读写文本文件很容易, 使用File Object即可操作, 读取字符串一般使用file.readline 或者 file.readlines, 一个是返回字符串对象, 一个是返回一个列表.

2.Python使用字符串对象存储二进制数据. 当调用file.read(n) 时, 返回一个字符串对象, 这个字符串对象类似于C++的 std::string 或者 char buf[], 都可以存储任何字节数据; 当需要对string对象,即字节数据进行操作时, 就需要unpack来进行数据转换, 比如把4个字节转换为数值, 把某部分的字节转换为str()字符串等等. Python的file对象类似于 C语言的FILE对象, 差不多有类似的对应函数.

3.对于struct.pack和unpack的说明, 其中 pack其实是对C结构体进行打包, 并进行本机默认地址对齐, 本地字节序. 比如 “bci” 的大小是8. 所以尽量使用i, 而不是b,c这些单字节, 因为被对齐后大小很难计算.

struct.pack(fmt, v1, v2, ...) 
Return a string containing the values v1, v2, ... packed according to the given format. The arguments must match the values required by the format exactly

struct.unpack(fmt, string) 
Unpack the string (presumably packed by pack(fmt, ...)) according to the given format. The result is a tuple even if it contains exactly one item. The string must contain exactly the amount of data required by the format (len(string) must equal calcsize(fmt)).

4.Byte Order, Size, and Alignment

CharacterByte orderSizeAlignment
@nativenativenative
=nativestandardnone
<little-endianstandardnone
>big-endianstandardnone
!network(= big-endian)standard

5.Format Characters

FormatC TypePython typeStandard sizeNotes
xpadbytenovalue
ccharstring of length11
bsigned charinteger1(3)
Bunsigned charinteger1(3)
?_Boolbool1(1)
hshortinteger2(3)
Hunsigned shortinteger2(3)
iintinteger4(3)
Iunsigned intinteger4(3)
llonginteger4(3)
Lunsigned longinteger4(3)
qlong longinteger8(2), (3)
Qunsigned long longinteger8(2), (3)
ffloatfloat4(4)
ddoublefloat8(4)
schar[]string
pchar[]string
Pvoid *integer(5), (3)

例子

#! encoding=utf8

import sys
import os
import io
from StringIO import  StringIO
from struct import unpack
from struct import pack

def TestWriter(path):
    f = open(path,"wb")

    # write png header
    header = pack('BBBB',0x89,0x50,0x4E,0x47)
    f.write(header)

    one_str = "string中文"
    one_char1 = 0
    one_char2 = ord('t')
    one_int = 50
    # we need calc string length
    str_len = len(one_str)
    format = '%dsiii' % (str_len)
    body = pack(format,one_str,one_char1,one_char2,one_int)
    print len(body)

    f.write(body)
    f.close()

def TestReader(path):
    f = open(path,"rb")
    header = f.read(1)
    a, = unpack("B",header)
    png = f.read(3)
    print png

    name=''
    n=f.read(1)
    while unpack('<b',n)[0]!=0:
        name=name+str(n)
        n=f.read(1)

    print name
    # three bytes. == 0
    f.seek(3,io.SEEK_CUR)

    left = f.read(8)
    a,b = unpack("ii",left)
    print "%d:%d" % (a,b)

    f.close()

if __name__ == '__main__':
    path = ""
    if len(sys.argv) > 1:
        path = sys.argv[1];
    else:
        path = "temp.png"

    TestWriter(path);
    TestReader(path);
    # os.remove(path)

输出

24
PNG
string中文
116:50

参考

struct – Interpret strings as packed binary data
Python使用struct处理二进制
File Objects

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

infoworld

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值