Answer the question
In order to leave comments, you need to log in
How to write hex to file?
Hello.
There is this code:
string ToHex(const string& s, bool upper_case)
{
ostringstream ret;
for (string::size_type i = 0; i < s.length(); ++i)
ret << hex << setfill('0') << setw(2) << (upper_case ? uppercase : nouppercase) << (int)s[i];
return ret.str();
}
int main(int argc, char* argv[]) {
ifstream::pos_type size;
char * memblock;
ifstream file ("program1.cs", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
}
string read_bytes = ToHex(memblock, true);
cout << read_bytes << endl;
string need_replace = "707269766174";
size_t pos = read_bytes.find(need_replace);
read_bytes.replace(pos, need_replace.length(), "7075626C6963");
ofstream replaced("rep.cs");
replaced << read_bytes; // ТУТ НУЖНА ПРАВИЛЬНАЯ ЗАПИСЬ В ФАЙЛ
replaced.close();
getchar();
}
707269766174
the hex address with 7075626C6963
. This address contains the word "privat". I will convert it to hex "public". But the output I get is not "public" converted from hex, but just HEX in the text. That is, the file contains this: "7075626C6963" ... I hope I managed to explain. Answer the question
In order to leave comments, you need to log in
Decided!
It was required to convert string
, in which hex is located, to unsigned char*
or unsigned char[]
, then write to a file in binary mode.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question