How can I convert this code from C into C++ ?
char out[61]; //null terminator
for (i = 0; i < 20; i++) {
    snprintf(out+i*3, 4, "%02x ", obuf[i])
}
I can't find any alternative for snprintf.
How can I convert this code from C into C++ ?
char out[61]; //null terminator
for (i = 0; i < 20; i++) {
    snprintf(out+i*3, 4, "%02x ", obuf[i])
}
I can't find any alternative for snprintf.
 
    
     
    
    Use stringstream class from <sstream>.
E.g.:
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
int main()
{
    stringstream ss;
    for (int i = 0; i < 20; i++) {
        ss << setw(3) << i;
    }
    cout << "Resulting string: " << endl;
    cout << ss.str() << endl;
    printf("Resulting char*: \n%s\n", ss.str().c_str() );
    return 0;
}
 
    
    This code is valid C++11, if you have #include <cstdio> and type std::snprintf (or using namespace std;).
No need to "fix" what isn't broken.
 
    
    You can use Boost.Format.
#include <boost/format.hpp>
#include <string>
std::string out;
for (size_t i=0; i<20; ++i)
    out += (boost::format("%02x") % int(obuf[i])).str();
 
    
    You can convert this code from C to C++ easily with standard library's std::stringstream and iomanip I/O stream manipulators:
#include <sstream>
#include <iomanip>
...
std::ostringstream stream;
stream << std::setfill('0') << std::hex;
for (const auto byte : obuf)
  stream << std::setw(2) << byte;
const auto out = stream.str();
