Answer the question
In order to leave comments, you need to log in
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();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question