fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5. #include <array>
  6. #include <cstdint>
  7. #include <iomanip>
  8.  
  9. template <typename Iter>
  10. std::string hex_str(Iter begin, Iter end)
  11. {
  12. std::ostringstream output;
  13. output << std::hex << std::setw(2) << std::setfill('0');
  14. while(begin != end)
  15. output << static_cast<unsigned>(*begin++);
  16. return output.str();
  17. }
  18.  
  19. template<typename C>
  20. std::string hex_str(const C &data) {
  21. return hex_str(data.begin(), data.end());
  22. }
  23.  
  24. int main() {
  25. std::array<uint8_t, 5> arr = {0x79, 0x6c, 0x65, 0x6f};
  26. std::cout << hex_str(arr.begin(), arr.end()) << std::endl;
  27. std::cout << hex_str(arr) << std::endl;
  28.  
  29. std::vector<uint8_t> vec = {0x79, 0x6c, 0x65, 0x6f};
  30. std::cout << hex_str(vec.begin(), vec.end()) << std::endl;
  31. std::cout << hex_str(vec) << std::endl;
  32. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
796c656f0
796c656f0
796c656f
796c656f