fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4.  
  5. unsigned char hex2dec(const std::string &s, size_t pos)
  6. {
  7. char ch = s[pos];
  8.  
  9. if (ch >= 'A' && ch <= 'F')
  10. return (ch - 'A') + 10;
  11.  
  12. if (ch >= 'a' && ch <= 'f')
  13. return (ch - 'a') + 10;
  14.  
  15. if (ch >= '0' && ch <= '9')
  16. return (ch - '0');
  17.  
  18. // error!
  19. return 0;
  20. }
  21.  
  22. unsigned char decodeHexByte(const std::string &s, size_t pos)
  23. {
  24. return (hex2dec(s, pos) << 4) | hex2dec(s, pos+1);
  25. }
  26.  
  27. int main()
  28. {
  29. std::string keyInStr = "1314191A1B";
  30. unsigned char keyInHex[5] = {};
  31.  
  32. for (int i = 0, j = 0; i < 5; ++i, j += 2)
  33. {
  34. keyInHex[i] = decodeHexByte(keyInStr, j);
  35. std::cout << std::hex << std::setw(2) << std::setfill('0') << (int) keyInHex[i] << " ";
  36. }
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
13 14 19 1a 1b