itoa的两种实现

简介:
一种是linux的实现,一种是Solaris的实现,代码如下:、
None.gif namespace linux
ExpandedBlockStart.gif {
InBlock.gif    void itoa( int i,charstring
ExpandedSubBlockStart.gif    {
InBlock.gif        int power, j;
InBlock.gif        j=i; 
InBlock.gif        for (power=1;j>=10;j/=10) 
InBlock.gif            power*=10; 
InBlock.gif        for (;power>0;power/=10)
ExpandedSubBlockStart.gif        {
InBlock.gif            *string++='0'+i/power; i%=power; 
ExpandedSubBlockEnd.gif        }

InBlock.gif        *string='\0';
ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif}


None.gif namespace solaris
ExpandedBlockStart.gif {
InBlock.gif    char * itoa(long n, int base
ExpandedSubBlockStart.gif    {
InBlock.gif        register char *p; 
InBlock.gif        register int minus; 
InBlock.gif        static char buf[36];
InBlock.gif        p = &buf[36]; 
InBlock.gif        *--p = '\0'; 
ExpandedSubBlockStart.gif        if (n < 0) { minus = 1; n = -n; } else minus = 0; 
InBlock.gif        if (n == 0)  *--p = '0'; 
ExpandedSubBlockStart.gif        else while (n > 0) { *--p = "0123456789abcdef"[n % base]; n /= base; } 
InBlock.gif        if (minus) *--p = '-';
InBlock.gif        return p;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


关键还是怎样去求整形的位数,求出来,剩下来的事情就很少了。
目录
相关文章
|
4月前
|
数据格式
sprintf函数
sprintf函数
18 0
|
9月前
strcmp与strncmp的实现和比较
strcmp与strncmp的实现和比较
52 0
|
10月前
|
Go API
【golang】strconv.Atoi 和 strconv.Itoa
【golang】strconv.Atoi 和 strconv.Itoa
141 0
|
10月前
itoa随手记(itoa是什么,itoa怎么用)
itoa随手记(itoa是什么,itoa怎么用)
111 0
|
11月前
|
Go 索引
Go 中的格式化字符串`fmt.Sprintf()` 和 `fmt.Printf()`
在 Go 中,可以使用 fmt.Sprintf() 和 fmt.Printf() 函数来格式化字符串,这两个函数类似于 C 语言中的 scanf 和 printf 函数。本文介绍了五个最常用的格式化动词和参数索引的使用方法。
125 0
atoi函数
atoi函数
119 0
itoa()函数与atoi()函数
1、itoa()函数(整型转字符) 以下是用itoa()函数将整数转换为字符串的一个例子: # include # include void main (void) { int num = 100; char str[...
1059 0
|
机器学习/深度学习

热门文章

最新文章