My favorite 2 generic integer to string functions are as follows. The first will convert a base 10 integer to a string, the second works for any base (e.g. binary (base 2), hex (16), oct (8) or decimal (10)):
/* simple base 10 only itoa */
char *
itoa10 (int value, char *result)
{
char const digit[] = "0123456789";
char *p = result;
if (value < 0) {
*p++ = '-';
value *= -1;
}
/* move number of required chars and null terminate */
int shift = value;
do {
++p;
shift /= 10;
} while (shift);
*p = '\0';
/* populate result in reverse order */
do {
*--p = digit [value % 10];
value /= 10;
} while (value);
return result;
}
any base number: (taken from http://www.strudel.org.uk/itoa/)
/* preferred itoa - good for any base */
char *
itoa (int value, char *result, int base)
{
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while (ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
Since it is done with pointers and without reliance on any C function that would require malloc, then I see do reason it wouldn't work in Pebble. However I am not familiar with Pebble.
char buf[0x100]; snprintf(buf, sizeof buf, "%d", i);
... – Forebode