分类: LINUX
2011-09-01 00:32:44
因为ANSI标准库里面并没有包含itoa函数,所以自己专门写了一个:
//------------------------------------------------------------------------------
//Modified on: 12/29, 2008
//Change return type of itoa. No meaningful to return copy address "s"
//------------------------------------------------------------------------------
#i nclude
static void reverse (char *);
//------------------------------------------------------------------------------
/// itoa: convert n to characters in s
///
//------------------------------------------------------------------------------
void itoa(int n, char *s)
{
int sign;
char *t = s;
if ((sign = n) < 0)
n = -n;
do
{
*s++ = n % 10 + '0';
}
while ((n /= 10) >0);
if (sign < 0)
*s++ = '-';
*s = '\0';
reverse(t);
}
//------------------------------------------------------------------------------
/// reverse: reverse sting s in place
///
//------------------------------------------------------------------------------
void reverse(char *s)
{
int c;
char *t;
for (t = s + (strlen(s) - 1); s < t; s++, t-- )
{
c = *s;
*s = *t;
*t = c;
}
}