在.bin文件中查找/替换长二进制模式 [英] finding/replacing a long binary pattern in a .bin file

查看:69
本文介绍了在.bin文件中查找/替换长二进制模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



..bin文件中找到二进制模式的常识方法是什么,比如大约200个字节,并用更新的模式替换它

相同偏移量的相同偏移量?


此外,模式可以出现在文件的任何字节边界上,所以

在16处通过代码进行分块字节一帧可能是个问题。

文件本身并不是那么大,可能只有32千字节,而且需要

的速度并不是那么大,但需要准确的/>
搜索/替换非常重要。


谢谢,


--Alan

What would be the common sense way of finding a binary pattern in a
..bin file, say some 200 bytes, and replacing it with an updated pattern
of the same length at the same offset?

Also, the pattern can occur on any byte boundary in the file, so
chunking through the code at 16 bytes a frame maybe a problem. The
file itself isn''t so large, maybe 32 kbytes is all and the need for
speed is not so great, but the need for accuracy in the
search/replacement is very important.

Thanks,

--Alan

推荐答案

2005年1月12日22:36:54 -0800,yaipa< ya *** @ yahoo.com>写道:
On 12 Jan 2005 22:36:54 -0800, yaipa <ya***@yahoo.com> wrote:

.bin文件中找到二进制模式的常识方法是什么,比如大约200个字节,并将其替换为更新的模式
同一长度在相同的偏移量?

此外,模式可以出现在文件的任何字节边界上,因此以16字节为单位对代码进行分块可能是一个问题。
文件本身并不是很大,可能只有32千字节,而且对速度的需求不是很大,但是对搜索/替换的准确性的需求非常重要。
What would be the common sense way of finding a binary pattern in a
.bin file, say some 200 bytes, and replacing it with an updated pattern
of the same length at the same offset?

Also, the pattern can occur on any byte boundary in the file, so
chunking through the code at 16 bytes a frame maybe a problem. The
file itself isn''t so large, maybe 32 kbytes is all and the need for
speed is not so great, but the need for accuracy in the
search/replacement is very important.




好​​的,给定了要求。


f = file(''mybinfile'')

contents = f.read()。replace(oldbinstring,newbinstring)

f.close()

f = file(''mybinfile'',''w' ')

f.write(内容)

f.close()


会这样做,并准确地做到。但它也会将整个

文件读入内存。


Stephen。



Okay, given the requirements.

f = file(''mybinfile'')
contents = f.read().replace(oldbinstring, newbinstring)
f.close()
f = file(''mybinfile'',''w'')
f.write(contents)
f.close()

Will do it, and do it accurately. But it will also read the entire
file into memory.

Stephen.


On Thu ,2005年1月13日16:51:46 +1000,Stephen Thorne< st ************ @ gmail.com>写道:
On Thu, 13 Jan 2005 16:51:46 +1000, Stephen Thorne <st************@gmail.com> wrote:
2005年1月12日22:36:54 -0800,yaipa< ya *** @ yahoo.com>写道:
On 12 Jan 2005 22:36:54 -0800, yaipa <ya***@yahoo.com> wrote:

.bin文件中找到二进制模式的常识方法是什么,比如大约200个字节,并将其替换为更新的模式
同一长度在相同的偏移量?

此外,模式可以出现在文件的任何字节边界上,因此以16字节为单位对代码进行分块可能是一个问题。
文件本身并不是很大,可能只有32千字节,而且对速度的需求不是很大,但是对搜索/替换的准确性的需求非常重要。
What would be the common sense way of finding a binary pattern in a
.bin file, say some 200 bytes, and replacing it with an updated pattern
of the same length at the same offset?

Also, the pattern can occur on any byte boundary in the file, so
chunking through the code at 16 bytes a frame maybe a problem. The
file itself isn''t so large, maybe 32 kbytes is all and the need for
speed is not so great, but the need for accuracy in the
search/replacement is very important.



好的,给定了要求。

f = file(''mybinfile'')
contents = f.read()。replace (oldbinstring,newbinstring)
f.close()
f = file(''mybinfile'',''w'')
f.write(内容)
f。关闭()

会这样做,并准确地做到这一点。但它也会将整个
文件读入内存。



Okay, given the requirements.

f = file(''mybinfile'')
contents = f.read().replace(oldbinstring, newbinstring)
f.close()
f = file(''mybinfile'',''w'')
f.write(contents)
f.close()

Will do it, and do it accurately. But it will also read the entire
file into memory.



你必须使用linux等,否则你会显示打开

_binary_文件(我认为这是'.s文件是什么)和''rb''和''wb'',IWT。


不确定OP是什么系统/是的。


顺便说一下,我确定你可以编写一个生成器,它将文件名

和oldbinstring和newbinstring作为参数,并且阅读并收益良好

os-file-system-friendly disk-sector-multiple chunk,所以你可以写


fout = open(''mynewbinfile' ',''wb'')

for update_file_stream(''myoldbinfile'',''rb'',oldbinstring,newbinstring):

fout.write( buf)

fout.close()


(留作练习;-)

(修改文件in地点是另一种练习)

(用定义的最大内存缓冲区usag做后者) e

即使mods增加文件的长度也是另一个;-)


问候,

Bengt Richter


[Stephen Thorne]
[Stephen Thorne]
2005年1月12日22:36:54 -0800,yaipa< ya *** @ yahoo。 COM>写道:
On 12 Jan 2005 22:36:54 -0800, yaipa <ya***@yahoo.com> wrote:
在.bin文件中找到二进制模式的常识方法是什么,比如大约200个字节,并用更新的
相同偏移的相同长度的图案?文件本身并不是很大,可能只有32千字节,对速度的需求并不是那么好,但对搜索/替换的准确性需求非常重要。
What would be the common sense way of finding a binary pattern in
a .bin file, say some 200 bytes, and replacing it with an updated
pattern of the same length at the same offset? The file itself
isn''t so large, maybe 32 kbytes is all and the need for speed is not
so great, but the need for accuracy in the search/replacement is
very important.


好​​的,考虑到要求。
f = file(''mybinfile'')
contents = f.read()。replace(oldbinstring,newbinstring)
f.close()
f = file('' mybinfile'',''w'')
f.write(内容)
f.close()
会这样做,并准确地做到。但它也会将整个
文件读入内存。

Okay, given the requirements. f = file(''mybinfile'')
contents = f.read().replace(oldbinstring, newbinstring)
f.close()
f = file(''mybinfile'',''w'')
f.write(contents)
f.close() Will do it, and do it accurately. But it will also read the entire
file into memory.




32Kb确实是一个小文件,在内存中读取它不是问题!


人们有时喜欢编写长Python程序。这是关于

相同,更短一些::-)


buffer = file(''mybinfile'',''rb'')。 read()。replace(oldbinstring,newbinstring)

file(''mybinfile'',''wb'')。write(buffer)


- -

Fran?ois Pinard http://pinard.progiciels-bpi .ca


这篇关于在.bin文件中查找/替换长二进制模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆