Skip to content

Instantly share code, notes, and snippets.

@TheEyesightDim
Last active September 5, 2019 12:02
Show Gist options
  • Save TheEyesightDim/e7dc0d8921cb27071dbce53dcccb1fe7 to your computer and use it in GitHub Desktop.
Save TheEyesightDim/e7dc0d8921cb27071dbce53dcccb1fe7 to your computer and use it in GitHub Desktop.
Converts some type into its representation as a byte array.
#include <algorithm>
#include <array>
template <typename T>
constexpr std::array<char, sizeof(T)> into_bytes(const T& t){
std::array<char, sizeof(T)> bytes;
std::copy( reinterpret_cast<const char*>(&t), reinterpret_cast<const char*>(&t) + sizeof(t), bytes.begin() );
return bytes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment