Skip to content

Instantly share code, notes, and snippets.

@lpcvoid
Created January 4, 2023 13:12
Show Gist options
  • Save lpcvoid/347f845f0f386cd63c83843faaff6b61 to your computer and use it in GitHub Desktop.
Save lpcvoid/347f845f0f386cd63c83843faaff6b61 to your computer and use it in GitHub Desktop.
IntConverter
struct IntConverter {
template <typename T>
static inline std::array<uint8_t, sizeof(T)> to_array(T integer, std::endian e) {
std::array<uint8_t, sizeof(T)> result{0};
// this is a bit old and dirty maybe, but it will do
if (e == std::endian::little) {
for (uint32_t i = 0; i < sizeof(T); ++i) {
result[i] = static_cast<uint8_t>(integer >> (i * 8));
}
} else {
for (uint32_t i = (sizeof(T) * 8); i > 0; i -= 8) {
result[(i / 8) - 1] = static_cast<uint8_t>(integer >> i);
}
}
return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment