fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include <string>
  4. #include <vector>
  5.  
  6. std::vector<uint8_t> convert(const std::string& s)
  7. {
  8. if (s.size() % 2 != 0) {
  9. throw std::runtime_error("Bad size argument");
  10. }
  11. std::vector<uint8_t> res;
  12. res.reserve(s.size() / 2);
  13. for (std::size_t i = 0, size = s.size(); i != size; i += 2) {
  14. std::size_t pos = 0;
  15. res.push_back(std::stoi(s.substr(i, 2), &pos, 16));
  16. if (pos != 2) {
  17. throw std::runtime_error("bad character in argument");
  18. }
  19. }
  20. return res;
  21. }
  22.  
  23. int main()
  24. {
  25. const char* starting = "001122AABBCC";
  26. std::vector<uint8_t> ending = {0x00, 0x11, 0x22, 0xAA, 0xBB, 0xCC};
  27.  
  28. std::cout << (ending == convert(starting)) << std::endl;
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
1