473,385 Members | 1,912 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.

C# Binary File Replacement

I'm wanting to replace Field Values in an existing PDF, I've done this
with PHP by doing a replace in the file.

I've been able to read the file in a byte array in c# but all my
attempts to replace strings in a binary file have led to failure for the
last couple of days.

I've stared at this function for too long I've become crosseyed ;)

Any suggestions, or is there an easier way to replace in a byte array og
which I'm not aware?
With thanks,
JDG

------------------ BEGIN CODE -------------------
private byte[] ReplaceInByteArray(byte[] OriginalArray, byte[] Find,
byte[] Replace)
{
byte[] ReturnValue = OriginalArray;

if(System.Array.BinarySearch(ReturnValue, Find) > -1 )
{
byte[] NewReturnValue;
int lFoundPosition;
int lCurrentPosition;
while(System.Array.IndexOf(ReturnValue, Find) > -1 )
{
NewReturnValue = new byte[ReturnValue.Length + Find.Length -
Replace.Length];
lFoundPosition = System.Array.IndexOf(ReturnValue, Find);
lCurrentPosition = 0;

for(int x = 0; x < lFoundPosition; x++)
{
NewReturnValue[x] = ReturnValue[x];
lCurrentPosition++;
}

for(int y = 0; y < Replace.Length; y++)
{
NewReturnValue[y + lCurrentPosition] = Replace[y];
}

lCurrentPosition = lCurrentPosition + Replace.Length;

while(lCurrentPosition < NewReturnValue.Length)
{
NewReturnValue[lCurrentPosition] = ReturnValue[lCurrentPosition -
Replace.Length + Find.Length];
lCurrentPosition++;
}

ReturnValue = NewReturnValue;
}
}

return ReturnValue;
}
------------------ END CODE -------------------
Nov 16 '05 #1
3 9201
No reason to return a new array. You can use the FileStream class to read a
single byte
at a time (it caches internally for optimum disk read buffer performance), match
it against
your find array, and then then set your position back to the beginning to easily
write out
your replacement array.

The issue would be if your replacement is longer than your find array. If this
is the case
some different methods would have to be used in order to *make room* for the
replacement.
Doing an inline file search and replace would definitely yield the fastest and
probably one of
the easiest algorithms.

Your own algorithm is truly flawed in that BinarySearch does not search for an
array of elements
within an array, but instead a single element. And it only does so correctly if
the array is sorted.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Jeffrey D. Gordon" <sp*******@goaway.com> wrote in message
news:Oo**************@tk2msftngp13.phx.gbl...
I'm wanting to replace Field Values in an existing PDF, I've done this
with PHP by doing a replace in the file.

I've been able to read the file in a byte array in c# but all my
attempts to replace strings in a binary file have led to failure for the
last couple of days.

I've stared at this function for too long I've become crosseyed ;)

Any suggestions, or is there an easier way to replace in a byte array og
which I'm not aware?
With thanks,
JDG

------------------ BEGIN CODE -------------------
private byte[] ReplaceInByteArray(byte[] OriginalArray, byte[] Find,
byte[] Replace)
{
byte[] ReturnValue = OriginalArray;

if(System.Array.BinarySearch(ReturnValue, Find) > -1 )
{
byte[] NewReturnValue;
int lFoundPosition;
int lCurrentPosition;
while(System.Array.IndexOf(ReturnValue, Find) > -1 )
{
NewReturnValue = new byte[ReturnValue.Length + Find.Length -
Replace.Length];
lFoundPosition = System.Array.IndexOf(ReturnValue, Find);
lCurrentPosition = 0;

for(int x = 0; x < lFoundPosition; x++)
{
NewReturnValue[x] = ReturnValue[x];
lCurrentPosition++;
}

for(int y = 0; y < Replace.Length; y++)
{
NewReturnValue[y + lCurrentPosition] = Replace[y];
}

lCurrentPosition = lCurrentPosition + Replace.Length;

while(lCurrentPosition < NewReturnValue.Length)
{
NewReturnValue[lCurrentPosition] = ReturnValue[lCurrentPosition -
Replace.Length + Find.Length];
lCurrentPosition++;
}

ReturnValue = NewReturnValue;
}
}

return ReturnValue;
}
------------------ END CODE -------------------

Nov 16 '05 #2
No reason to return a new array. You can use the FileStream class to read a
single byte
at a time (it caches internally for optimum disk read buffer performance), match
it against
your find array, and then then set your position back to the beginning to easily
write out
your replacement array.

The issue would be if your replacement is longer than your find array. If this
is the case
some different methods would have to be used in order to *make room* for the
replacement.
Doing an inline file search and replace would definitely yield the fastest and
probably one of
the easiest algorithms.

Your own algorithm is truly flawed in that BinarySearch does not search for an
array of elements
within an array, but instead a single element. And it only does so correctly if
the array is sorted.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Jeffrey D. Gordon" <sp*******@goaway.com> wrote in message
news:Oo**************@tk2msftngp13.phx.gbl...
I'm wanting to replace Field Values in an existing PDF, I've done this
with PHP by doing a replace in the file.

I've been able to read the file in a byte array in c# but all my
attempts to replace strings in a binary file have led to failure for the
last couple of days.

I've stared at this function for too long I've become crosseyed ;)

Any suggestions, or is there an easier way to replace in a byte array og
which I'm not aware?
With thanks,
JDG

------------------ BEGIN CODE -------------------
private byte[] ReplaceInByteArray(byte[] OriginalArray, byte[] Find,
byte[] Replace)
{
byte[] ReturnValue = OriginalArray;

if(System.Array.BinarySearch(ReturnValue, Find) > -1 )
{
byte[] NewReturnValue;
int lFoundPosition;
int lCurrentPosition;
while(System.Array.IndexOf(ReturnValue, Find) > -1 )
{
NewReturnValue = new byte[ReturnValue.Length + Find.Length -
Replace.Length];
lFoundPosition = System.Array.IndexOf(ReturnValue, Find);
lCurrentPosition = 0;

for(int x = 0; x < lFoundPosition; x++)
{
NewReturnValue[x] = ReturnValue[x];
lCurrentPosition++;
}

for(int y = 0; y < Replace.Length; y++)
{
NewReturnValue[y + lCurrentPosition] = Replace[y];
}

lCurrentPosition = lCurrentPosition + Replace.Length;

while(lCurrentPosition < NewReturnValue.Length)
{
NewReturnValue[lCurrentPosition] = ReturnValue[lCurrentPosition -
Replace.Length + Find.Length];
lCurrentPosition++;
}

ReturnValue = NewReturnValue;
}
}

return ReturnValue;
}
------------------ END CODE -------------------

Nov 16 '05 #3
These are the two functions I wrote to make this work for me.

Thanks to Justin Rogers for showing me the light in how Binary Search
works, after sleeping on it I decided to write my own byte array search
function.
----------------------- BEGIN CODE -------------------------------
private byte[] ReplaceInByteArray(byte[] OriginalArray, byte[] Find,
byte[] Replace)
{
byte[] ReturnValue = OriginalArray;

if(System.Array.BinarySearch(ReturnValue, Find) > -1 )
{
byte[] NewReturnValue;
int lFoundPosition;
int lCurrentPosition;
int lCurrentOriginalPosition;
while(FindInByteArray(ReturnValue, Find) > -1 )
{
NewReturnValue = new byte[ReturnValue.Length + Replace.Length -
Find.Length];
lFoundPosition = FindInByteArray(ReturnValue, Find);
lCurrentPosition = 0;
lCurrentOriginalPosition = 0;

for(int x = 0; x < lFoundPosition; x++)
{
NewReturnValue[x] = ReturnValue[x];
lCurrentPosition++;
lCurrentOriginalPosition++;
}

for(int y = 0; y < Replace.Length; y++)
{
NewReturnValue[lCurrentPosition] = Replace[y];
lCurrentPosition++;
}

lCurrentOriginalPosition = lCurrentOriginalPosition + Find.Length;

while(lCurrentPosition < NewReturnValue.Length)
{
NewReturnValue[lCurrentPosition] =
ReturnValue[lCurrentOriginalPosition];
lCurrentPosition++;
lCurrentOriginalPosition++;
}

ReturnValue = NewReturnValue;
}

}
return ReturnValue;
}

private int FindInByteArray(byte[] Haystack, byte[] Needle)
{
int lFoundPosition = -1;
int lMayHaveFoundIt = -1;
int lMiniCounter = 0;

for(int lCounter = 0; lCounter < Haystack.Length; lCounter++)
{
if(Haystack[lCounter] == Needle[lMiniCounter])
{
if(lMiniCounter == 0)
lMayHaveFoundIt = lCounter;
if(lMiniCounter == Needle.Length - 1)
{
return lMayHaveFoundIt;
}
lMiniCounter++;
}
else
{
lMayHaveFoundIt = -1;
lMiniCounter = 0;
}
}

return lFoundPosition;
}
------------------------- END CODE -------------------------------
Nov 16 '05 #4

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

Similar topics

13
by: yaipa | last post by:
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...
5
by: Robert Manea | last post by:
Hello everyone, I wrote, simply as an exercise, a small piece of code to find 'strings' (defined as an amount of at least 3 ASCII characters followed by a non ASCII character) in binary files. ...
2
by: Dave Veeneman | last post by:
I'm working on a project where I have to persist data to a file, rather than to a database. Basically, I need to save the state of several classes, each of which will have a couple of dozen...
0
by: Jeffrey D. Gordon | last post by:
I'm wanting to replace Field Values in an existing PDF, I've done this with PHP by doing a replace in the file. I've been able to read the file in a byte array in c# but all my attempts to...
29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
6
by: py_genetic | last post by:
Hi, I'm looking to generate x alphabetic strings in a list size x. This is exactly the same output that the unix command "split" generates as default file name output when splitting large...
27
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.