山狼啸月 发表于 2013-10-22 22:39

itoa() 函数的转换原理是什么啊

static char *itoa(int value, char *string, int radix)
{   
    int   i, d;   
    int   flag = 0;   
    char    *ptr = string;   
   /* This implementation only works for decimal numbers. */   
        if (radix != 10)   
        {   
      *ptr = 0;   
      return string;   
        }   

        if (!value)   
        {   
      *ptr++ = 0x30;   
      *ptr = 0;   
      return string;   
        }   

        /* if this is a negative value insert the minus sign. */   
        if (value < 0)   
        {   
      *ptr++ = '-';   
      /* Make the value positive. */   
      value *= -1;   
        }   
        for (i = 10000; i > 0; i /= 10)   
        {   
      d = value / i;   
      if (d || flag)   
      {   
          *ptr++ = (char)(d + 0x30);   
          value -= (d * i);   
          flag = 1;   
      }   
        }   

/* Null terminate the string. */   
*ptr = 0;   
return string;   
}/* NCL_Itoa */
求换算原理

li880wert 发表于 2013-10-25 10:43

不就是 字符'2'=0x32数字 2 =0x2;

如 value = 222   ==>"222";

其他代码 自己看看就明白了
for (i = 10000; i > 0; i /= 10)==> int 表示的最大位数 9


/* if this is a negative value insert the minus sign. */   
          if (value < 0)   ==> 负数 -

山狼啸月 发表于 2013-10-29 20:36

li880wert 发表于 2013-10-25 10:43 static/image/common/back.gif
不就是 字符'2'=0x32数字 2 =0x2;

如 value = 222   ==>"222";


看明白了!

山狼啸月 发表于 2013-10-29 20:36

li880wert 发表于 2013-10-25 10:43 static/image/common/back.gif
不就是 字符'2'=0x32数字 2 =0x2;

如 value = 222   ==>"222";


看明白了!
页: [1]
查看完整版本: itoa() 函数的转换原理是什么啊