V
V
VirusesAnalystCoder2020-06-27 14:52:37
C++ / C#
VirusesAnalystCoder, 2020-06-27 14:52:37

C++. Change HEX in binary file?

Hello.
I found a similar question on stackoverflow - https://stackoverflow.com/questions/25625115/cpp-b...
But I didn't quite understand how to do it, that's why I'm asking a question.
I also want to note the difference between the questions: that question is only reading, and I need to know how to write as well.
Below is a screenshot of the HEX text. It's not a binary file, but that's not the point. I have highlighted what I need to change and I have highlighted WHAT I need to change.

https://imgur.com/a/iLX06Q0

There is this code from stackoverflow:

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main() {
    vector<unsigned char> bytes1{0x70, 0x72, 0x69, 0x76, 0x61, 0x74};
    ofstream outfile("program1.cs", std::ios::binary);
    outfile.write((char*)&bytes1[0], bytes1.size());
    outfile.close();

    vector<unsigned char> bytes2(bytes1.size(), 0);
    ifstream infile("program1.cs", ios::in | ios::binary);
    infile.read((char*)&bytes2[0], bytes2.size());
    for (int i = 0; i < bytes2.size(); ++i) {
        printf("%02X ", bytes2[i]);
    }

    getchar();
}

PS: I have already replaced the bytes I needed from the screenshots in the bytes1 vector.
I will be grateful for any help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-06-27
@VirusesAnalystCoder

Everything just go to the position of the text (dares the first letter) and write - it will be written over this text, but you need to check that the number of bytes written is no more than what needs to be changed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question