C++ most efficient way of transforming string to hex string -


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, ' ');     (int = 0; < len; ++i) {         s[2 * i] = hexmap[(data[i] & 0xf0) >> 4];         s[2 * + 1] = hexmap[data[i] & 0x0f];     }     return s; } 

however, know if there more efficient way of doing this. i'd know if there easier ways, if so, performance trade-offs present.

avoiding possible dynamic memory allocation more efficient strings on few chars long.

a way avoid when possible, let caller pass buffer place result in, , provide default wrapper using dynamic allocation of suitable buffer (e.g. std::string, in code).

however, unless measurements show function absolutely critical performance-wise, that's wasted work: it's not matter of course.


Comments