python写二进制文件_python使用struct库对二进制文件进行读写

很多时候二进制文件常常用16进制代表一个 大的正整数。

例如:956506想转入到文件中如何写入呢?

with open('test.bin',mode = 'wb+') as f:

f.seek(0x4,0)

f.write(struct.pack('i',956506))

最终写入的结果是 i的 4字符内存占位方式 即 5E 98 0E 00 ,低16位在前,高16位在后

956506转化为hex = 0e985E

struct.pack 是用来打包结构体的内存中实际占位情况。

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

>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()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值