如何使用 C# 编辑二进制文件的十六进制值

发布于 2024-09-09 01:40:05 字数 267 浏览 6 评论 0原文

这是我的问题。我有一个要编辑的二进制文件。我当然可以使用十六进制编辑器来编辑它,但我需要编写一个程序来编辑这个特定的文件。假设我知道要编辑的某个十六进制,我知道它的地址等。假设它是一个 16 位二进制,地址是 00000000,它位于第 04 行,值为 02。我如何创建一个可以更改该十六进制值的程序,并且只需单击按钮即可更改该十六进制值?

我找到了谈论类似事情的资源,但我一生都无法找到解决确切问题的帮助。

任何帮助将不胜感激,并且请不要只是告诉我答案(如果有的话),而是尝试解释一下。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

血之狂魔 2024-09-16 01:40:05

我认为最好用一个具体的例子来解释这一点。以下是 Visual Studio 的十六进制编辑器中所示的可执行文件的前 32 个字节:

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

现在,文件实际上只是字节的线性序列。您在十六进制编辑器中看到的行只是为了使内容更易于阅读。当您想要使用代码操作文件中的字节时,您需要通过从 0 开始的位置来识别字节。在上面的示例中,非零字节的位置如下:

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

在上面所示的十六进制编辑器表示中,左侧的数字表示相应行中第一个字节的位置。编辑器每行显示 16 个字节,因此每行增加 16 (0x10)。

如果您只想获取文件中的一个字节并更改其值,我认为最有效的方法是使用 FileStream 打开文件,查找适当的位置,然后覆盖该字节。例如,以下命令会将位置 24 处的 0x40 更改为 0x04:

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

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);
}
涫野音 2024-09-16 01:40:05

那么第一件事可能是理解转换。十六进制到十进制可能并不那么重要(当然,除非您需要首先更改十进制的值,但这是一个简单的转换公式),但十六进制到二进制将很重要,因为每个十六进制字符(0-9,AF ) 对应于特定的二进制输出。

了解这些内容后,下一步是准确找出您要搜索的内容,进行正确的转换,并替换该确切的字符串。我建议(如果缓冲区不会太大)获取整个十六进制转储并替换您在其中搜索的任何内容,以避免覆盖重复的二进制序列。

希望有帮助!

问候,
丹尼斯·M.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文