Created
January 4, 2023 13:12
-
-
Save lpcvoid/347f845f0f386cd63c83843faaff6b61 to your computer and use it in GitHub Desktop.
IntConverter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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