Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace a byte in a binary file using perl

Status
Not open for further replies.

subok

MIS
Feb 21, 2005
37
BE
I've been trying but no success. I want to replace byte in a binary file and save a backup file :

Where a byte sequence in the file is : EE 09 12 C3

will be replaced by : EE D9 12 C3
(09--> D9)

Is it possible?

Thanks for your help.

 
you'll have to binmode the file before reading it ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I've been trying...
If you show us the code you have so far, it will be easier for us to understand the level of help you require.


Yours,

fish

["]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life was going to be spent in finding mistakes in my own programs.["]
--Maurice Wilkes, 1949
 
Try the following at the prompt:

perl -pi.bak -e 's{\xEE\x09\x12\xC3}{\xEE\xD9\x12\xC3}' file.bin


where "file.bin" is the name of the file(s) you want to change.



Trojan.
 
Trojan's code will replace the first occurrence of this sequence, are you sure this is what you want? Also changing any bytes in a file may cause the CRC check to fail resulting in a useless file, unless of course you reverse the codes above and hope that a similar occurrence hasn't happened before.

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top