Skip to content

Instantly share code, notes, and snippets.

@scriptum
Last active December 23, 2015 20:18
Show Gist options
  • Save scriptum/6688203 to your computer and use it in GitHub Desktop.
Save scriptum/6688203 to your computer and use it in GitHub Desktop.
itoa - convert int to char *
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