Getting a buffer into a stringstream in hex representation:
Look at the stream modifiers: std::setw
and std::setfill
. It will help you.
#include <sstream>
#include <iomanip>
std::stringstream ss;
ss << std::hex << std::setfill('0');
for (int i = 0; i < 32; ++i)
{
ss << std::setw(2) << static_cast<unsigned>(buffer[i]);
}
You can do this with C++20 std::format
:
std::stringstream ss;
for (int i = 0; i < 32; ++i) {
ss << std::format("{:02}", buffer[i]);
}
Until std::format
is widely available you can use the {fmt} library, std::format
is based on. {fmt} also provides the join
function that makes this even easier (godbolt):
std::string s = fmt::format("{:02}", fmt::join(buffer, ""));
Disclaimer: I'm the author of {fmt} and C++20 std::format
.