Replacing a specific hex offset in C#

不想你离开。 提交于 2019-12-08 09:37:56

问题


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.



来源:https://stackoverflow.com/questions/10231234/replacing-a-specific-hex-offset-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!