Tuesday, April 26, 2016

C++: Convert a string to its hex form


#include <sstream>
#include <iomanip>

...

std::string toHex(const std::string &input)
{
    std::stringstream ss;
    ss.setf(ios::uppercase);
    ss << hex;

    for (size_t i = 0; i < input.length(); i++)
        ss << setw(2) << setfill('0') 
           << (unsigned short)(input[i] & 0xff);

    return ss.str();
}
 

No comments:

 
Get This <