How to edit a binary file's hex value using C#

18,034

Solution 1

I think this is best explained with a specific example. Here are the first 32 bytes of an executable file as shown in Visual Studio's hex editor:

00000000  4D 5A 90 00 03 00 00 00  04 00 00 00 FF FF 00 00
00000010  B8 00 00 00 00 00 00 00  40 00 00 00 00 00 00 00

Now a file is really just a linear sequence of bytes. The rows that you see in a hex editor are just there to make things easier to read. When you want to manipulate the bytes in a file using code, you need to identify the bytes by their 0-based positions. In the above example, the positions of the non-zero bytes are as follows:

Position  Value
--------  ------
  0        0x4D
  1        0x5A
  2        0x90
  4        0x03
  8        0x04
 12        0xFF
 13        0xFF
 16        0xB8
 24        0x40

In the hex editor representation shown above, the numbers on the left represent the positions of the first byte in the corresponding line. The editor is showing 16 bytes per line, so they increment by 16 (0x10) at each line.

If you simply want to take one of the bytes in the file and change its value, the most efficient approach that I see would be to open the file using a FileStream, seek to the appropriate position, and overwrite the byte. For example, the following will change the 0x40 at position 24 to 0x04:

using (var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite)) {
    stream.Position = 24;
    stream.WriteByte(0x04);
}

Solution 2

Well the first thing would probably be to understand the conversions. Hex to decimal probably isn't as important (unless of course you need to change the value from a decimal first, but that's a simple conversion formula), but hex to binary will be important seeing as each hex character (0-9,A-F) corresponds to a specific binary output.

After understanding that stuff, the next step is to figure out exactly what you are searching for, make the proper conversion, and replace that exact string. I would recommend (if the buffer wouldn't be too large) to take the entire hex dump and replace whatever you're searching for in there to avoid overwriting a duplicate binary sequence.

Hope that helps!

Regards,
Dennis M.

Share:
18,034
JC Leyba
Author by

JC Leyba

I like computers, but I don't like them THAT much. That's why I'm here, to get help when things don't work out so well for me, or to help when I can. If my answers have helped you out, maybe drop by my website and have a look around? printf("Wazzup! \n");

Updated on June 11, 2022

Comments

  • JC Leyba
    JC Leyba almost 2 years

    So here's my issue. I have a binary file that I want to edit. I can use a hex editor to edit it of course, but I need to make a program to edit this particular file. Say that I know a certain hex I want to edit, I know it's address etc. Let's say that it's a 16-bit binary, and the address is 00000000, it's on row 04 and it has a value of 02. How could I create a program that would change the value of that hex, and only that hex with the click of a button?

    I've found resources that talk about similar things, but I can't for the life of me find help with the exact issue.

    Any help would be appreciated, and please, don't just tell me the answer if there is one but try and explain a bit.

    • Admin
      Admin almost 14 years
      Do I get it correclty that you want to overwrite a few bytes at a given location in a file already on disk?
  • JC Leyba
    JC Leyba almost 14 years
    Thank you so much, I was able to complete my program with this information.