My case is as follows:
- I have a binary file that I am reading using the read operation std :: fstream as (char *)
- My goal is to take every byte from the file, format it in hexadecimal format, and then add it to the string variable
- The string variable must contain all the contents of the file formatted as element 2.
For example, let's say I have the following binary file:
D0 46 98 57 A0 24 99 56 A3
The formatting method for each byte is as follows:
stringstream fin;; for (size_t i = 0; i < fileb_size; ++i) { fin << hex << setfill('0') << setw(2) << static_cast<uint16_t>(fileb[i]); }
The above approach works as expected, however for large files it is very slow, which I understand; stringstream is for formatting input!
My question is, are there any ways to optimize such code or the approach that I take together? My only limitation is that the output must be in string format, as shown above.
Thanks.
source share