Skip to content

Instantly share code, notes, and snippets.

@cstroie
Created April 11, 2019 14:04
Show Gist options
  • Save cstroie/3d49ed5898af0a95a33fd939647ca240 to your computer and use it in GitHub Desktop.
Save cstroie/3d49ed5898af0a95a33fd939647ca240 to your computer and use it in GitHub Desktop.
bin2bcd
uint32_t bin2bcd(const uint16_t in) {
uint32_t out = 0;
for (uint8_t pos = 13; pos >= 0; pos--) {
if ((out & 0xF) >= 5)
out += 3;
if (((out & 0xF0) >> 4) >= 5)
out += (3 << 4);
if (((out & 0xF00) >> 8) >= 5)
out += (3 << 8);
out = (out << 1) | ((in >> pos) & 1);
}
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment