Solved

How to replace some bytes of a binary file

Posted on 2003-10-29
5
502 Views
Last Modified: 2012-05-07
     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
Comment
Question by:mscprojhk
5 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9641953
There is probably some logic you use to decide which bytes to replace and with what bytes exactly. You did not give details so I cannot help you with this.

Here is however some info which classes to use:

- FileStream with the file name and the needed file mode to specify and open the file
- BinaryReader to read from the file
- BinaryWriter to write to the file

For more info see the documentation of the FileStream constructor (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofilestreamclassctortopic2.asp). You can find here also specific topics for reading/writing to a file.

You can also directly write to and read from the FileStream. For more info see the members Read() and Write().
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9645763
Do you want to search for a byte sequence or do you know the position?
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 50 total points
ID: 9646494
Here is some real quick code that reads a binary file byte by byte until it finds the two characters you are looking for.  After it reads each byte it outputs it to a new file, replacing the chosen byte sequence with a new sequence of numbers.

Obviously this can be streamlined quite a bit by using byte arrays and reading an entire buffer and then testing the elements in the array.  But this is simple and it gets the job done.



                  byte[] replacement = {0x1b, 0x26, 0x66, 0x31, 0x30, 0x30, 0x59, 0x1b, 0x26, 0x66, 0x58};

                  byte temp;

                  Console.WriteLine(Convert.ToChar(temp));

                  FileStream inStream = new FileStream(@"C:\temp\test_in.dat", FileMode.Open);
                  FileStream outStream = new FileStream(@"C:\temp\test_out.dat", FileMode.Create);
                  BinaryReader br = new BinaryReader(inStream);
                  BinaryWriter bw = new BinaryWriter(outStream);
                  
                  while(br.BaseStream.Position<br.BaseStream.Length)
                  {
                        temp = br.ReadByte();
                        if(temp == 0x1b)
                        {
                              if(br.PeekChar()==Convert.ToInt16(0x25))
                              {
                                    temp = br.ReadByte();
                                    bw.Write(replacement);
                                    bw.Flush();
                              }
                        }
                        else
                        {
                              bw.Write(temp);
                              bw.Flush();
                        }
                  }
0
 

Author Comment

by:mscprojhk
ID: 9647389
This is what I want after modified a bit.

But, if I want to replace not only TWO bytes, not any number of bytes generically, any approaches?
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9648373



Sure... The best method would be to Not read each byte one at a time, but to actually read in a few hundred K into an array, and the scan the array for the special sequence you are looking for.


The only thing that you really need is a pattern or a particular series of bytes you want to remove.

Let's suppose you want to remove any series that looks like any one of the following

{0x82, 0x34, 0x65, 0xAF}
{0x23, 0xCA, 0x67}
(0x27, 0x17, 0x99, 0x54, 0xA3, 0xF4, 0x3C}


and replace all of those bytes with
{0xFF, 0xFF}

The easiest method would be to load all of your bytes into an array list (if you are not familiar with arraylists, they are like arrays only you can add and remove data from them and they can change size).

Unfortunately since I am not at work I don't have VS.Net in front of me, however I do know that there is a Find() function that requires a value and a starting point and an ending point.

So you simply search for 0x82 and when you find it, see if myArray[n+1]  == 0x34, etc...

If it does, remove the appropriate number of cells and then insert your values.  

Then when you are done you just convert back to a byte array

byte[] bArr = myArrList.ToArray(byte);  //??
byte[] bArr = (byte)myArrList.ToArray();  //??

//like I said, I don't have the IDE in front of me so I'm afraid I can't be as precise as I'd like.  I'll check back in tomorrow when I am at work and I'll have better code then
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now