473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Find and Replace in Binary File

Newbie here. How do I do a find and replace in a binary file? I need
to read in a binary file then replace a string "ABC" with another
string "XYZ" then write to a new file. Find string is the same length
as Replace string. Here's what I have so far. I spent many hours
googling for sample code but couldn't find much. Thanks...

public static void FindReplace(string OldFile, string NewFile)
{
string sFind = "ABC"; //I probably need to convert these
to a byte array
string sReplace = "XYZ"; //but I don't know how.
int i;
FileStream fin = new FileStream(OldFile, FileMode.Open);
FileStream fout = new FileStream(NewFile,
FileMode.Create);
do
{
i = fin.ReadByte();
if (i != -1)
{
//I think I need to compare the byte being read in
//to the 1st sFind byte array here. If it matches
then
//store the position and compare the next byte.
//If all 3 bytes match then replace with sReplace
byte array.
//I'm just not sure how to do it.
fout.WriteByte((byte)i);
}
} while (i != -1);
fin.Close();
fout.Close();
}
Feb 24 '08 #1
3 14493
I have done this before. The approach I took was to read the file bytes
into memory (because the files I was working with were small), move the
file out of the way, and then write a new file replacing the "find"
bytes with the "replace" bytes. So similar to your approach.

Here's a snippet from what I wrote (it was in 1.1 and was just "quick
and dirty" code):

ArrayList newBytes = new ArrayList(bytes.Length);
int ndx = 0;

for (int x = 0; x < bytes.Length; x++) // bytes is the original files bytes
{
if (bytes[x] == findBytes[ndx]) // findBytes is a byte[] from the
"find" string
{
if (ndx == findBytes.Length - 1)
{
for (int y = 0; y < replaceBytes.Length; y++) //
replaceBytes is a byte[] from the "replace" string
{
newBytes.Add(replaceBytes[y]);
}
ndx = 0;
}
else
{
ndx++;
}
}
else
{
if (ndx 0)
{
for (int y = 0; y < ndx; y++)
{
newBytes.Add(findBytes[y]);
}
}
ndx = 0;
newBytes.Add(bytes[x]);
}
}

ndx is used to keep track of which byte in the findBytes should be
compared to the original files byte.

mo*****@yahoo.com wrote:
Newbie here. How do I do a find and replace in a binary file? I need
to read in a binary file then replace a string "ABC" with another
string "XYZ" then write to a new file. Find string is the same length
as Replace string. Here's what I have so far. I spent many hours
googling for sample code but couldn't find much. Thanks...

public static void FindReplace(string OldFile, string NewFile)
{
string sFind = "ABC"; //I probably need to convert these
to a byte array
string sReplace = "XYZ"; //but I don't know how.
int i;
FileStream fin = new FileStream(OldFile, FileMode.Open);
FileStream fout = new FileStream(NewFile,
FileMode.Create);
do
{
i = fin.ReadByte();
if (i != -1)
{
//I think I need to compare the byte being read in
//to the 1st sFind byte array here. If it matches
then
//store the position and compare the next byte.
//If all 3 bytes match then replace with sReplace
byte array.
//I'm just not sure how to do it.
fout.WriteByte((byte)i);
}
} while (i != -1);
fin.Close();
fout.Close();
}
Feb 24 '08 #2
Thanks for all your comments. Here's what I'm trying to mimic in
VB6. I guess .NET is more restrictive in what type of file you can do
find/replace. Sounds too difficult in C# so I might just have to
revert back to VB6 for this one. Thanks...

Sub FindRplace()
Dim sBuffer As String
Dim ff As Integer
ff = FreeFile
Open txtFile.Text For Binary As #ff
sBuffer = Space$(LOF(ff))
Get #ff, 1, sBuffer
Close #ff
sBuffer = Replace(sBuffer, "ABC", "XYZ")
Open txtNew.Text For Binary As #ff
Put #ff, , sBuffer
Close #ff
End Sub
Feb 25 '08 #3
On Mon, 25 Feb 2008 11:56:50 -0800, <mo*****@yahoo.comwrote:
Thanks for all your comments. Here's what I'm trying to mimic in
VB6. I guess .NET is more restrictive in what type of file you can do
find/replace. Sounds too difficult in C# so I might just have to
revert back to VB6 for this one.
It's not difficult in C#. The code would not even be significantly
different from the VB6 code you posted, other than the lack of a
short-hand "replace" feature for dealing with byte arrays (which you
should be able to easily write yourself). And your VB6 code has all the
same problems we've warned that could come up doing it in C#.

But if you've already got VB6 code to do what you want, maybe you should
just stick with that if you'd rather not use the suggestions offered here.

Pete
Feb 25 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: hokieghal99 | last post by:
import os, string print " " setpath = raw_input("Enter the path: ") def find_replace(setpath): for root, dirs, files in os.walk(setpath): fname = files for fname in files: find =...
1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
19
by: rbt | last post by:
Here's the scenario: You have many hundred gigabytes of data... possible even a terabyte or two. Within this data, you have private, sensitive information (US social security numbers) about your...
6
by: Jon Slaughter | last post by:
I'm trying to replace some bytes in a file using fstream but all I seem to be able to do is append or 0 the file then write... Basicaly I just need to replace the first 512 bytes of the file with...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
3
by: Mullin Yu | last post by:
I have a file, and want to replace some bytes by other bytes e.g. Old: 1b 25 New: 1b 26 66 31 30 30 59 1b 26 66 58 How to implement? Thanks!
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
23
by: Umesh | last post by:
This is a basic thing. Say A=0100 0001 in ASCII which deals with 256 characters(you know better than me!) But we deal with only four characters and 2 bits are enough to encode them. I want to...
40
by: niskin | last post by:
Hi I'm trying to open up a .jpg file in binary mode using C and replace it with another .jpg file. This is my code so far: #include <stdio.h> #include <stdlib.h> int main() { FILE...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.