Last active
December 23, 2015 20:18
-
-
Save scriptum/6688203 to your computer and use it in GitHub Desktop.
itoa - convert int to char *
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
char *itoa(int i) | |
{ | |
static char buf[12]; | |
int j = 11; | |
int sign = 0; | |
buf[j] = '\0'; | |
if(i < 0) | |
{ | |
i = -i; | |
sign = 1; | |
} | |
do | |
{ | |
j--; | |
buf[j] = i % 10 + '0'; | |
i /= 10; | |
} while(i); | |
if(sign) | |
{ | |
j--; | |
buf[j] = '-'; | |
} | |
return buf + j; | |
} | |
char *uitoa(unsigned int i) | |
{ | |
static char buf[12]; | |
int j = 11; | |
buf[j] = '\0'; | |
do | |
{ | |
j--; | |
buf[j] = i % 10 + '0'; | |
i /= 10; | |
} while(i); | |
return buf + j; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment