Replacing a specific hex offset in C#

2019-06-09 04:05发布

问题:

How would I go about replacing the content of a specific hex offset in a binary file with C#?

To make it more clear, lets say my offset is 0x33347, and the content of it is 06. I would want to change 06 to 17. How do I do this? I have very little experience with hex editing, so I weren't really able to figure anything out myself, I'm kind of lost.

回答1:

Using a FileStream, set the Position of the stream to the offset, then write the byte.

This will overwrite the current content with what you want.

using(var fs = new FileStream("path to file", 
                              FileMode.Open, 
                              FileAccess.ReadWrite))
{
    fs.Position = 0x33347;
    fs.WriteByte(Convert.ToByte(0x6));
}


回答2:

Open the stream in read-write mode, read up to your offset (or seek if your stream supports seeking), write your byte, flush and close the stream.



标签: c# hex offset