fork(1) download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. char hextob(char ch)
  9. {
  10. if (ch >= '0' && ch <= '9') return ch + '0';
  11. if (ch >= 'A' && ch <= 'F') return ch - 10 + 'A';
  12. if (ch >= 'a' && ch <= 'f') return ch - 10 + 'a';
  13. return 0;
  14. }
  15. template<typename T>
  16. T hextot(char* hex)
  17. {
  18. T value = 0;
  19. for (size_t i = 0; i < sizeof(T)*2; ++i)
  20. value |= hextob(hex[i]) << (8*sizeof(T)-4*(i+1));
  21. return value;
  22. };
  23.  
  24. int main()
  25. {
  26.  
  27. char str[2] = {'3','6'};
  28. std::cout << hextot<int16_t>(str) << "\n";
  29.  
  30. }
  31.  
  32.  
Success #stdin #stdout 0.01s 5476KB
stdin
Standard input is empty
stdout
30208