posts - 225, comments - 62, trackbacks - 0, articles - 0
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

字符串(char *)转整型(int)

Posted on 2006-05-30 22:22 魔のkyo 阅读(1595) 评论(1)  编辑 收藏 引用 所属分类: Programming
/*
函数原型:
int strtoi(const char *str,int base);

描述:
将字符串转换为10进制无符号整数返回

参数:
const char *str为需要转换的字符串,应由0-9和A-Z组成
int base字符串的进制(基数),应为2-36

返回值:
返回传入字符串所代表的10进制无符号整数,如果参数错误无法转换返回-1
*/

int  strtoi( const   char   * str, int   base )
{
    
int  res = 0 ,t;
    
const   char   * p;
    
for (p = str; * p;p ++ ){
        
if (isdigit( * p)){
            t
=* p - ' 0 ' ;
        }
        
else   if (isupper( * p)){
            t
=* p - ' A ' + 10 ;
        }
        
else  {
            
return   - 1 ;
        }
        
if (t >= base ) return   - 1 ;
        res
*= base ;
        res
+= t;
    }
    
return  res;
}
strtoi.txt

Feedback

# re: 字符串(char *)转整型(int)  回复  更多评论   

2009-12-29 02:41 by www.541fanwen.cn
www.541fanwen.cn
只有注册用户登录后才能发表评论。