Skip to content

Instantly share code, notes, and snippets.

@patrickelectric
Last active September 25, 2020 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickelectric/217800f9cf9a7007bd7e8befe3630f41 to your computer and use it in GitHub Desktop.
Save patrickelectric/217800f9cf9a7007bd7e8befe3630f41 to your computer and use it in GitHub Desktop.
#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 (&parameters)[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