Last active
September 25, 2020 18:16
-
-
Save patrickelectric/217800f9cf9a7007bd7e8befe3630f41 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <cstdint> | |
#include <cstdio> | |
namespace version_helper { | |
/** | |
* @brief Create the header sequence with checksum based in a string | |
* | |
* @tparam Type: Desired type of header | |
* @tparam N: Size of string | |
* @return Type: Header sequence created with checksum | |
*/ | |
template<typename Type, size_t N> | |
constexpr Type create_header(const char (¶meters)[N]) { | |
const uint8_t size = sizeof(Type); | |
static_assert(N <= size, "Type can't hold string size."); | |
uint8_t checksum = 0; | |
Type result = 0; | |
for(uint8_t i = 0; i < N ; i++) { | |
const Type value = static_cast<Type>(parameters[i]); | |
checksum += value; | |
result += static_cast<Type>(value << 8*(size - i - 1)); | |
} | |
result += checksum; | |
return result; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment