I followed the tutorial on cplusplus.com for std::ofstream::binary and cannot seem to get the hex output that I need in binary mode.
I currently need to do a project in cryptography and require the output to be in binary mode (and not writing the hex values into the contents of the file).
Below is an excerpt from my code (with comments):
ofstream writeout(filename2.c_str(), ios::binary);
//ctextvec is the vector that stores the ciphertext
for (int i=0; i<ctextvec.size(); i+=2)
{
ostringstream oss, oss2;
//formatting of hex to string (adding missing '0')
oss << hex << setw(16) << setfill('0') << ctextvec[i];
oss2 << hex << setw(16) << setfill('0') << ctextvec[i+1];
//Stores all ciphertext characters as a string
storestr.append(oss.str());
storestr.append(oss2.str());
}
//Define new array to store characters for writing
char writearr[storestr.size()];
for(int i=0; i<storestr.size(); i++) writearr[i]=storestr[i];
//Perform write to file
for(int i=0; i<storestr.size(); i++)
writeout.write((char*)&writearr[i],sizeof(char));
writeout.flush();
writeout.close();
I tried a lot of methods and all seemed to dump the contents into a file rather than writing into binary mode. I supposedly need this command to work to show that a file is encrypted (at least).
Below are the screenshots of the file that I got and what I should be getting.
Any help is much appreciated!


ctextvec.data(), most of your code is unnecessary.