none
Trying to write Hex to a file RRS feed

  • Question

  • Hey, I'm trying to write a program that changes the hex of a file based on what the user enters.
    Example:
    User enters C0
    program changes header 4000c053 in xyz.exe to C0

    It doesn't have to be very complex

    Im really new to this so.... thanks in advance
    Monday, July 17, 2006 2:58 AM

All replies

  • When you're editing existing exe file, I believe you should replace some bytes with the length of bytes, for example 4000c053 is 8 bytes while C0 is only 2 bytes. Is this what you're really want to do ?
    Monday, July 17, 2006 3:53 AM
  •  Andoko wrote:
    When you're editing existing exe file, I believe you should replace some bytes with the length of bytes, for example 4000c053 is 8 bytes while C0 is only 2 bytes. Is this what you're really want to do ?


    Well, I meant to change the hex AT 4000C053 from 51 TO C0.
    I'm not trying to write a hex editor or anything, it's just, the file the program will edit can only be edited with hex, so I'm trying to find a simple script that will read the hex from a file and then let me change the hex in the file... hope you caught that 
    Thanks for the reply
    Monday, July 17, 2006 1:58 PM
  • It looks to like you're after a "patcher" type program.

    For this, I'd look to the System.IO namespace and the BinaryReader/Writer classes.  In effect you'll need a program that does something along these (psuedocode - don't wanna spoil the challange ) lines:


    Reader = "myInputFile"

    Writer = "myOutputFile"

    While(moreBytesLeftToReadInFile)

    {

    Byte byteData = Reader.Readbyte(currentBytePointer)

    if(currentBytePointer == theByteIWantToChange)

    byteData = myNewValueForThisByte

    Writer.Writebyte(currentBytePointer, byteData)

    currentBytePointer++

    }

    CloseEverythingAndDispose()


    Once you've processed all the bytes in the file, you can overwrite the original file, (myInputFile) with your new file, (myOutputFile).

    If you really, really need an actual example I can give you one, but you just want hints right? 

     (one other thing to note is that the "location" you've used in your example looks more like a memory location to me, you'll need the "fileOffset" of the byte in question - basically the position of the byte as a number, where the start of the file equals 0). 

    Good Luck!

    Friday, July 21, 2006 6:41 PM
  • Yeah, an example would be great if you don't mind . I think I'm starting to get the hang of it, but not quite :\ . Thanks again!
    Saturday, July 29, 2006 11:01 PM
  • Ok, I'm Back with one more qUestion... With the BinaryReader/Writer in the systeM namesPace, is it possible to  read/write hex?  thanks again
    Tuesday, August 1, 2006 12:21 AM