字符串和整数互转

来源:互联网 发布:mpu6050中文数据手册 编辑:程序博客网 时间:2024/05/16 19:46
字符串转整数
http://www.cnblogs.com/processakai/archive/2011/06/24/2089348.html
http://www.cppblog.com/prayer/archive/2010/09/06/126014.html
atoi

isspace(int x)
{
 if(x==''||x=='\t'||x=='\n'||x=='\f'||x=='\b'||x=='\r')
  return 1;
 else  
  return 0;
}
isdigit(int x)
{
 if(x<='9'&&x>='0')         
  return 1;x` 
 else 
  return 0;

}
int atoi(const char *nptr)
{
       intc;             
       inttotal;        
       intsign;          

       
       while ( isspace((int)(unsigned char)*nptr) )
           ++nptr;

       c = (int)(unsigned char)*nptr++;
       sign =c;          
       if (c == '-' || c == '+')
           c = (int)(unsignedchar)*nptr++;   

       total = 0;

       while (isdigit(c)) {
           total = 10 * total + (c -'0');    
           c = (int)(unsignedchar)*nptr++;   
       }

       if (sign == '-')
           return -total;
       else
           return total;  
}


整数转字符串

itoa

char str[50];

sprintf(str,"%d",n);

原创粉丝点击