Created
May 5, 2015 21:22
-
-
Save markpapadakis/d44eb8b53071a1390dbc 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
static uint8_t DigitsCount(uint64_t v) | |
{ | |
for (uint8_t res{1}; ;res+=4, v/=10000U) | |
{ | |
if (likely(v < 10)) return res; | |
if (likely(v < 100)) return res + 1; | |
if (likely(v < 1000)) return res + 2; | |
if (likely(v < 10000)) return res + 3; | |
} | |
} | |
uint8_t U32ToASCII(uint32_t v, char *const dst) | |
{ | |
const auto result = DigitsCount(v); | |
uint32_t pos = result - 1; | |
while (v >= 10) | |
{ | |
const auto q = v / 10; | |
const auto r = static_cast<uint32_t>(v % 10); | |
dst[pos--] = '0' + r; | |
v = q; | |
} | |
dst[pos] = static_cast<uint32_t>(v) + '0'; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment