Created
April 9, 2014 10:55
-
-
Save fjolnir/10254472 to your computer and use it in GitHub Desktop.
This file contains 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
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