I have this:
std::string TCPMessengerServer::hexStr(unsigned char *data, int len)
{
constexpr char hexmap[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
std::string s(len * 2, ' ');
for (int i = 0; i < len; ++i) {
s[2 * i] = hexmap[(data[i] & 0xF0) >> 4];
s[2 * i + 1] = hexmap[data[i] & 0x0F];
}
return s;
}
However, I would like to know if there is a more efficient way of doing this. I'd also like to know if there are easier ways, but if so, what performance trade-offs they present.
std::ostringstreamand thestd::hexstream manipulator?