I am trying to read a file which contains bytes into a hex string.
std::ifstream infile("data.txt", std::ios_base::binary);
int length = 10;
char char_arr[length];
for (int i=0; i<length; i++)
{
infile.get(char_arr[i]);
}
std::string hex_data(char_arr);
However the hex_data does not look like a hex string. Is there a way to convert the bytes to a hex string during reading?
std::stringconstructor that you are using requires a null terminatedchararray.int length = 10; char char_arr[length];is not standard C++, see Why aren't variable-length arrays part of the C++ standard?. Either makelengthconst, or else usenew[]or betterstd::vector.char*by itself. There is another constructor that accepts a lengthstd::string. But that is pretty moot in this case.