Reading Editing and Saving Hex Files

Mar 25, 2008 at 3:06pm
Hi all, been a long long time since i did any programming and i was shaky then at best. I got a little program i want to write, and i was wondering if anyone could give me an example or two to get me started. Basically here is what i want to do. Open a hex file, search for a certain value, replace that value, then save and close the hex file. Any help would be much appreciated.
Thanks!
Mar 25, 2008 at 3:13pm
closed account (z05DSL3A)
http://www.cplusplus.com/doc/tutorial/files.html
Mar 25, 2008 at 3:58pm
Thank you for the reply grey wolf. I actually already printed that, and can open and save my file. What would be a good approach to searching and replacing a 12 byte hex number now that i have the file loaded into memory?
Mar 25, 2008 at 5:17pm
There's no such thing as a "hex file". It's just a way to represent files as text so that you can view or edit it's contents visually. You must open the file as "ios::binary".

Here's a suggestion, using I/O iterators:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// "iterator.cpp"
#include <fstream>
#include <iterator>
using namespace std;

int main() {
    typedef istream_iterator<unsigned char> input_iter_t;
    typedef ostream_iterator<unsigned char> output_iter_t;

    const off_t SIZE = 12;
    char before[SIZE] = { 0x69, 0x6e, 0x70, 0x75, 0x74, 0x5f, 0x69, 0x74, 0x65, 0x72, 0x5f, 0x74 };
    char after[SIZE] = { 0x69, 0x74, 0x65, 0x72, 0x5f, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x5f, 0x74 };

    fstream f("iterator.cpp", ios::binary | ios::in | ios::out);

    if (search(input_iter_t(f), input_iter_t(), before, before + SIZE) != input_iter_t()) {
        f.seekp(-SIZE, ios::cur); // I don't like this
        f.write(after, SIZE);
    }
}


What it does is that it finds the sequence of "hex" bytes in the array "before", and replaces them with the bytes in the array "after". In this case, it operates on the source file itself, and replaces "input_iter_t" in line 7 with "iter_input_t".

It works, and I think using iterators is a nice way to search a file. However, I don't like the hacking with seekp() to be able to write back to the same position. I'm not sure it's portable either.

I think with some deeper study of I/O iterators and standard algorithms, even simpler methods can be found.
Last edited on Mar 25, 2008 at 5:19pm
Mar 25, 2008 at 5:26pm
Thank you for the example Ropez, very helpful. Yes i understand that i am working with a binary file, just mentioned hex b/c that is how i usually view the data. Im sure ill have some more questions, but this should keep me occupied for a bit. Thanks again, much appreciated.
Mar 25, 2008 at 7:18pm
Okay, got your example working Ropez, now i think i need to modify it a bit. Now, say i want to again, open a file and search for 12 known bytes, but this time, i am interested in the 12 bytes that follow the known 12 bytes. Once i have these bytes i will close the file and open a second file and again search fo r the 12 known bytes, then replace the following 12 with the 12 i found it the first file. Only thing i am not clear on is how i save the 12 bytes from the first file? Thanks again
Mar 25, 2008 at 9:53pm
>> Only thing i am not clear on is how i save the 12 bytes from the first file? Thanks again

You just use read instead of write:
1
2
3
char buffer[SIZE];
// ... seek to correct position ...
f.read(buffer, SIZE);
Mar 26, 2008 at 7:38am
Have you ever thought about using a hex editor instead of using a c++ compiler? And if you know the string you want to replace you just use the find function. Search on google for a free hex editor.
Mar 26, 2008 at 10:17am
Thanks ropez!

Ewu, i usually do use a hex editor, however im trying to automate the process, but thank you for the suggestion.
Topic archived. No new replies allowed.