一种是linux的实现,一种是Solaris的实现,代码如下:、
关键还是怎样去求整形的位数,求出来,剩下来的事情就很少了。
namespace linux
{
void itoa( int i,char* string)
{
int power, j;
j=i;
for (power=1;j>=10;j/=10)
power*=10;
for (;power>0;power/=10)
{
*string++='0'+i/power; i%=power;
}
*string='\0';
}
}
{
void itoa( int i,char* string)
{
int power, j;
j=i;
for (power=1;j>=10;j/=10)
power*=10;
for (;power>0;power/=10)
{
*string++='0'+i/power; i%=power;
}
*string='\0';
}
}
namespace solaris
{
char * itoa(long n, int base)
{
register char *p;
register int minus;
static char buf[36];
p = &buf[36];
*--p = '\0';
if (n < 0) { minus = 1; n = -n; } else minus = 0;
if (n == 0) *--p = '0';
else while (n > 0) { *--p = "0123456789abcdef"[n % base]; n /= base; }
if (minus) *--p = '-';
return p;
}
}
{
char * itoa(long n, int base)
{
register char *p;
register int minus;
static char buf[36];
p = &buf[36];
*--p = '\0';
if (n < 0) { minus = 1; n = -n; } else minus = 0;
if (n == 0) *--p = '0';
else while (n > 0) { *--p = "0123456789abcdef"[n % base]; n /= base; }
if (minus) *--p = '-';
return p;
}
}
关键还是怎样去求整形的位数,求出来,剩下来的事情就很少了。