Skip to content

Instantly share code, notes, and snippets.

@fjolnir
Created April 9, 2014 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjolnir/10254472 to your computer and use it in GitHub Desktop.
Save fjolnir/10254472 to your computer and use it in GitHub Desktop.
uint16_t itoa(int16_t num, unsigned char *buf, uint16_t maxlen)
{
int ofs = maxlen;
unsigned char *head = buf;
int16_t abs_num = abs(num);
do {
*head++ = "0123456789"[abs_num % 10];
abs_num /= 10;
} while(abs_num != 0);
if(num < 0) *head++ = '-';
// Terminate&reverse the string
*head-- = '\0';
unsigned char *l = buf;
unsigned char *r = head;
while(l < r) {
unsigned char tmp = *r;
*r-- = *l;
*l++ = tmp;
}
return head - buf + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment