Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1818675
  • 博文数量: 473
  • 博客积分: 13997
  • 博客等级: 上将
  • 技术积分: 5953
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-22 11:52
文章分类

全部博文(473)

文章存档

2014年(8)

2013年(38)

2012年(95)

2011年(181)

2010年(151)

分类: 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;
  }
}

阅读(716) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~