fork download
  1. #include <iostream>
  2.  
  3. std::string hex2bin(const std::string& hex) {
  4. std::string bin;
  5. for (unsigned int i = 0; i < hex.length(); i+=2) {
  6. std::string byteString = hex.substr(i, 2);
  7. char byte = static_cast<char>(stoi(byteString, nullptr, 16));
  8. bin += byte;
  9. }
  10. return bin;
  11. }
  12.  
  13. char HexToByte(char c) {
  14. if (c - '0' < 10) {
  15. return c - '0';
  16. } else {
  17. return c - 'a' + 10;
  18. }
  19. }
  20.  
  21. std::string HexStringToBinary(const std::string& hex) {
  22. std::string ret;
  23. ret.reserve((hex.size() + 1) / 2);
  24.  
  25. size_t i = 0;
  26. if (hex.size() % 2) {
  27. ret += HexToByte(hex[i++]);
  28. }
  29.  
  30. while (i < hex.size()) {
  31. char c = HexToByte(hex[i++]);
  32. c <<= 4;
  33. c |= HexToByte(hex[i++]);
  34. ret += c;
  35. }
  36.  
  37. return ret;
  38. }
  39.  
  40. int main() {
  41. std::string result = "e99a18c428cb38d5f260853678922e03";
  42. std::string binaryStr = hex2bin(result);
  43. std::cout << binaryStr << std::endl;
  44. std::string res = "\xE9\x9A\x18\xC4\x28\xCB\x38\xD5\xF2\x60\x85\x36\x78\x92\x2E\x03";
  45. std::cout << res << std::endl;
  46.  
  47. std::string k = HexStringToBinary(result);
  48. std::cout << k << std::endl;
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
��(�8��`�6x�.
��(�8��`�6x�.
��(�8��`�6x�.