简体   繁体   English

C#写入十六进制文件

[英]c# Write to hex file

Just made a mod installer to modify hex of a game file but it doesn't seem to write it correctly this is the code I use to write it: 刚刚制作了一个mod安装程序来修改游戏文件的十六进制,但似乎无法正确编写,这是我用来编写该文件的代码:

BinaryWriter BWriter = new BinaryWriter(File.OpenWrite(HexFile));


        for (int i = value; i < value2; i++)
        {
            BWriter.BaseStream.Position = i;
            BWriter.Write(NewHex);
        }

And this is the "NewHex" that it should write: 这是它应该写的“ NewHex”:

1A 00 00 00 04 0B 19 01 4F 02 00 00 0A 00 00 00 00 00 00 1B 66 03 00 00 00 00 00 00 16 53 00 00

But it doesn't do that, instead, it converts that hex string to binary and then writes it to the file resulting in something totally different AND it writes a bunch of gibberish to the beginning: before the hex string, in this case 2 lines made up out of 61 were place before that hex thus breaking the game 但是它并没有这样做,而是将十六进制字符串转换为二进制,然后将其写入文件,从而产生完全不同的内容,并且在开始处写了一堆乱码:在十六进制字符串之前,在这种情况下为2行由61组成,这些位置在该十六进制之前,因此破坏了游戏

Does anyone know a solution to this? 有人知道解决方案吗?

Thanks 谢谢

EDIT: Fixed the first issue, now writes correctly, this is the new code: 编辑:修复了第一个问题,现在可以正确写入,这是新代码:

 private void button4_Click(object sender, EventArgs e)
    {
        int value = (int)new System.ComponentModel.Int32Converter().ConvertFromString(StartAddr);
        int value2 = (int)new System.ComponentModel.Int32Converter().ConvertFromString(StopAddr);


        //int StopInt = int.Parse(StopAddr);
        BinaryWriter BWriter = new BinaryWriter(File.OpenWrite(HexFile));


        for (int i = value; i < value2; i++)
        {
            BWriter.BaseStream.Position = i;
            BWriter.Write(StringToByteArray(NewHex));
        }

    }
    public static byte[] StringToByteArray(string hex)
    {
        return Enumerable.Range(0, hex.Length)
                         .Where(x => x % 2 == 0)
                         .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                         .ToArray();
    }

I can see that it wrote the hex correctly now but there is still one issue, it writes this: 我可以看到它现在正确地写入了十六进制,但是仍然存在一个问题,它写道:

1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 1A 00 00 00 04 0B 19 01 4F 02 00 00 0A 00 00 00 00 00 00 1B 66 03 00 00 00 00 00 00 16 53 00 00

Instead of this: 代替这个:

1A000000040B19014F0200000A0000000000001B660300000000000016530000

Does anyone know a solution for this problem? 有人知道这个问题的解决方案吗?

I think StringToByteArray is working properly. 我认为StringToByteArray工作正常。 However I can see that you loop 但是我看到你循环

for (int i = value; i < value2; i++)
{
    BWriter.BaseStream.Position = i;
    BWriter.Write(StringToByteArray(NewHex));
}

will gradually write 1A000000040B19014F0200000A0000000000001B660300000000000016530000 to stream starting at each of stream position from value to value2 . 将逐步将1A000000040B19014F0200000A0000000000001B660300000000000016530000valuevalue2每个流位置开始的流。 Are sure you need this loop? 确定需要这个循环吗?

In your code, the Write method is the String overloaded version, which means it convert the exact type you pass it (string) to binary. 在您的代码中,Write方法是String重载版本,这意味着它将您传递的确切类型(字符串)转换为二进制。 The BinaryWriter.Write(String) overload - it prefixes the string with a single-byte length, which is not what you want. BinaryWriter.Write(String)重载-它以单字节长度为字符串加上前缀,这不是您想要的长度。 Therefore you will not see the hex value as you want to see in the file. 因此,您将看不到想要在文件中看到的十六进制值。

BWriter.Write(Encoding.ASCII.GetBytes(NewHex)); BWriter.Write(Encoding.ASCII.GetBytes(NewHex));

This would help you as well. 这也会对您有帮助。 C#: Write values into Binary (.bin) file format C#:将值写入二进制(.bin)文件格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM