domlao domlao - 2 months ago 26
C++ Question

Use stream instead of snprintf

I'm not fairly familiar in C++ stream API and I want to convert a C code using stream in C++,

char sHex[20] = {0};
int numid = 2;

snprintf( sHex, sizeof(sHex) - 1, "%X", numId );

Answer
stringstream ss;
ss << uppercase << hex << numId;
string res = ss.str();
Comments