-2

I'm trying to replace only one byte of data from a file, meaning something like 0X05 -> 0X15. I'm using Replace function to do this.

using (StreamReader reader = new System.IO.StreamReader(Inputfile))
        {
            content = reader.ReadToEnd();
            content = content.Replace("0x05","0x15");
            reader.Close();
        }
        using (FileStream stream = new FileStream(outputfile, FileMode.Create))
        {
            using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8))
            {
                writer.Write(content);
            }
        }

Technically speaking, only that byte of data had to replaced with new byte, but I see there are many bytes of data changed.

Why other bytes are changing ?How can I achieve this?

Anonymous
  • 1
  • 1
  • 1
    Note that you are replacing the _string_ "0x05" with _the string_ "0x15". Anyway, can you show the file contents before and after you run the code, to illustrate what you mean by "many bytes of data changed"? – Sweeper Apr 20 '21 at 04:11
  • Does https://stackoverflow.com/a/28607981/34092 work? – mjwills Apr 20 '21 at 04:22
  • 2
    You should use `StreamWriter` rather than `BinaryWriter`. The [`Write(string)`](https://learn.microsoft.com/en-us/dotnet/api/system.io.binarywriter.write?view=net-5.0#System_IO_BinaryWriter_Write_System_String_) method of `BinaryWriter` will prefix string with a binary encoding of its length (the number of bytes used for this depends on the length of the string being written). Frankly, it's probably better to use `FIle.ReadAllText(Inputfile)` and `File.WriteAllText(content)` to do what you're trying to. – ProgrammingLlama Apr 20 '21 at 04:36
  • I understand.I changed my approach and fixed the issue. – Anonymous Apr 21 '21 at 07:05

1 Answers1

1

You're talking about bytes but you've written code that reads strings; strings are an interpretation of bytes so if you truly do mean bytes, mangling them through strings is the wrong way to go

Anyways, there are helper methods to make your life easy, if the file is relatively small (maybe up to 500mb - I'd switch to using an incremental streaming reading/changing/writing method if it's bigger than this)

If you want bytes changed:

var b = File.ReadAllBytes("path");
for(int x = 0; x < b.Length; x++) 
  if(b[x] == 0x5)
    b[x] = (byte)0x15;
File.WriteAllBytes("path", b);

If your file is a text file that literally has "0x05" in it:

File.WriteAllText("path", File.ReadAllText("path").Replace("0x05", "0x15"));

In response to your question in the comments, and assuming you want your file to grow by 2 bytes more for each 0x05 it contains (so a 1000 byte file that cotnains three 0x05 bytes will be 1006 bytes after being written) it is probably simplest to:

var b = File.ReadAllBytes("path");

using(FileStream fs = new FileStream("path", FileMode.Create)) //replace file
{
  for(int x = 0; x < b.Length; x++) 
    if(b[x] == 0x5) {
      fs.WriteByte((byte)0x15);
      fs.WriteByte((byte)0x5);
      fs.WriteByte((byte)0x15);
    } else
      fs.WriteByte(b);
}

Don't worry about writing a single byte at a time - it is buffered elsewhere in the IO chain. You could go for a solution that writes blocks of bytes from the array if you wanted.. this is just easier to code/understand

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • I figured out a similar solution and able to fix my issue.Thanks for the support. But any suggestion on what to do when a new byte of data has to be added before and after specific byte of data. What I mean is where ever 0x05 is there in a file 0x15 has to be added before and after that, something like 0x09 0x0A 0x05 0x0A => 0x09 0x0A 0x15 0x05 0x15 0x0A – Anonymous Apr 21 '21 at 07:12
  • Do you mean you want your file to grow 2 bytes longer for each 0x05 it contains? – Caius Jard Apr 21 '21 at 08:51