Given an (unsigned) integer, what is the generally fastest way to convert it into a string that contains its decimal representation?
The naïve way of doing that is repeatedly dividing by 10, until you reach zero. I dislike this approach, because it
- uses integer division, which is both slow and not available on some integrated platforms
- requires the programmer to flip the string afterwards. This doubles the number of memory operations needed.
I thought of the following method to convert integers to decimal base. Is this a good idea? How is this done in common implementations of functions like printf
?
#include <stdint.h>
const static uint64_t i64_tab[20] = {
1u,
10u,
100u,
1000u,
10000u,
100000u, /* 10^ 5 */
1000000u,
10000000u,
100000000u,
1000000000u,
10000000000u, /* 10^10 */
100000000000u,
1000000000000u,
10000000000000u,
100000000000000u,
1000000000000000u, /* 10^15 */
10000000000000000u,
100000000000000000u,
1000000000000000000u,
10000000000000000000u /* 10^19 */
};
void uint64_to_string(char *out, uint64_t in) {
int i;
uint64_t tenpow;
char accum;
for (i = 19;i > 0;i--) {
if (in >= i64_tab[i]) break;
}
do {
tenpow = i64_tab[i];
accum = '0';
while (in >= tenpow) {
in -= tenpow;
accum++;
}
*out++ = accum;
} while (i --> 0);
*out = '\0';
}
const static uint32_t i32_tab[10] = {
1u,
10u,
100u,
1000u,
10000u,
100000u, /* 10^ 5 */
1000000u,
10000000u,
100000000u,
1000000000u, /* 10^9 */
};
void uint32_to_string(char *out, uint32_t in) {
int i;
uint32_t tenpow;
char accum;
for (i = 9;i > 0;i--)
if (in >= i32_tab[i]) break;
do {
tenpow = i32_tab[i];
accum = '0';
while (in >= tenpow) {
in -= tenpow;
accum++;
}
*out++ = accum;
} while (i --> 0);
*out = '\0';
}