fork download
  1. #include <iostream>
  2. #include <array>
  3. #include <algorithm>
  4.  
  5. template<typename T>
  6. auto serialize(T n) -> std::array<unsigned char, sizeof n>
  7. {
  8. std::array<unsigned char, sizeof n> bytes;
  9. std::copy(reinterpret_cast<const char *>(&n),
  10. reinterpret_cast<const char *>(&n) + sizeof n,
  11. bytes.begin());
  12. return bytes;
  13. }
  14.  
  15. int main()
  16. {
  17. auto arr = serialize(static_cast<uint64_t>(0x123456));
  18. for (auto byte : arr)
  19. std::cout << std::hex << +byte << " ";
  20. }
Success #stdin #stdout 0s 4384KB
stdin
Standard input is empty
stdout
56 34 12 0 0 0 0 0