fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. std::string string_to_hex(const std::string& in) {
  7. std::stringstream ss;
  8.  
  9. // setfill adds padded zeros at the beginning
  10. ss << std::hex << std::setw(2) << std::setfill('0');
  11.  
  12. for (size_t i = 0; in.length() > i; ++i) {
  13. ss << static_cast<unsigned int>(in[i]);
  14. }
  15.  
  16. return ss.str();
  17. }
  18. int main()
  19. {
  20. std::cout << string_to_hex("abc") << std::endl;
  21. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
616263