Skip to content

Instantly share code, notes, and snippets.

@niorko
Created February 2, 2016 12:59
Show Gist options
  • Select an option

  • Save niorko/6f150c86d1f9db7ef4b4 to your computer and use it in GitHub Desktop.

Select an option

Save niorko/6f150c86d1f9db7ef4b4 to your computer and use it in GitHub Desktop.
Alternative to sprintf for pebble SDK (no need for stdio.h)
char *itoa(int num)
{
static char buff[20] = {};
int i = 0, temp_num = num, length = 0;
char *string = buff;
if (num >= 0) {
while(temp_num) {
temp_num /= 10;
length++;
}
for (i = 0; i < length; i++) {
buff[(length-1)-i] = '0' + (num % 10);
num /= 10;
}
buff[i] = '\0'; // can't forget the null byte to properly end our string
}
else {
return "Unsupported Number";
}
return string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment