itoa实现

1. 字符串转换为十进制整数; 字符串形式为"     -1234";

 

2. 实现

int itoa(char *str)

{

  int nRet=0;

  bool minus=false;

  if(NULL==str)

    return nRet;

  

  //空格判断

  while(' '==*str)

    ++str;

  

  //正负号判断

  if('-'==*str)

  {

    minus=true;

    ++str;

  }

  if('+'==*str)

  {

    minus=true;

    ++str;

  }

 

  while( *str>'0' && *str<'9')

  {

    //注意nRet溢出整形能表达的范围

    nRet=nRet*10+*str-'0';

    ++str;

  }

 

  if(minus)

    nRet=-1*nRet;

  

  return nRet;

}

posted @ 2015-04-18 17:13  hy1hy  阅读(248)  评论(0编辑  收藏  举报