简体   繁体   中英

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:

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:

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

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. 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 . 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. The BinaryWriter.Write(String) overload - it prefixes the string with a single-byte length, which is not what you want. Therefore you will not see the hex value as you want to see in the file.

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

This would help you as well. C#: Write values into Binary (.bin) file format

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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