fork(5) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. unsigned char val(char c)
  5. {
  6. if ('0' <= c && c <= '9') { return c - '0'; }
  7. if ('a' <= c && c <= 'f') { return c + 10 - 'a'; }
  8. if ('A' <= c && c <= 'F') { return c + 10 - 'A'; }
  9. throw "Eeek";
  10. }
  11.  
  12. std::string decode(std::string const & s)
  13. {
  14. if ((s.size() % 2) != 0) { throw "Eeek"; }
  15.  
  16. std::string result;
  17. result.reserve(s.size() / 2);
  18.  
  19. for (std::size_t i = 0; i < s.size(); i+=2)
  20. {
  21. unsigned char n = val(s[i]) * 16 + val(s[i + 1]);
  22. result += n;
  23. }
  24.  
  25. return result;
  26. }
  27.  
  28. int main ()
  29. {
  30. string hex = "537461636b6f766572666c6f77206973207468652062657374212121";
  31. string decoded;
  32. decoded = decode(hex);
  33. cout << decoded;
  34. }
  35.  
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
Stackoverflow is the best!!!