C++ converting vector to 2 hex and then store it in a string -
i have been using c pretty new c++. want convert int vector (std::vector) hexadecimal representation , store string. found use in c in following thread: converting hex string using 'sprintf'
the code proposed user411313 following:
static unsigned char digest[16]; static unsigned char hex_tmp[16]; (i = 0; < 16; i++) { printf("%02x",digest[i]); sprintf(&hex_tmp[i], "%02x", digest[i]); }
one of concerns go out of index @ point since sprintf may try add 0 after content. also, wondering if there way native c++, perhaps built function used instead of c functions. preferable in c++ on c functions? thank assistance!
if there way native c++, perhaps built function used instead of c functions. preferable in c++ on c functions?
sure there way, , yes it's preferable:
static std::array<unsigned char,16> digest; static std::string hex_tmp; (auto x : digest) { std::ostringstream oss; oss << std::hex << std::setw(2) << std::setfill('0') << (unsigned)x; hex_tmp += oss.str(); }
one of concerns go out of index @ point since sprintf may try add 0 after content.
that's valid concern. classes used in code snippet above overcome these issues, , don't need care about.
Comments
Post a Comment