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

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

Posted on 2006-05-30 22:19 魔のkyo 阅读(3218) 评论(0)  编辑 收藏 引用 所属分类: Programming

 

/*
函数原型:
char *itostr(int n, int base);

描述:
传入十进制的n返回base进制的字符串,空间是函数内的static空间

参数:
int n        将要转换的10进制整数
int base    需要返回的字符串的进制(基数),应为2-36

返回值:
返回base进制的字符串,由阿拉伯数字和大写字母组成
*/

char   * itostr( int  n,  int   base )
{
    register 
char   * p;
    register 
int  minus;
    
static   char  buf[ 36 ];
    
    p 
=   & buf[ 36 ];
    
*-- =   0 ;
    
if  (n  <   0 ) {
        minus 
=   1 ;
        n 
=   - n;
    }
    
else
        minus 
=   0 ;
    
if  (n  ==   0 )
        
*-- =   ' 0 ' ;
    
else
        
while  (n  >   0 ) {
            
*-- =   " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ " [n  %   base ];
            n 
/=   base ;
        }
    
if  (minus)
        
*-- =   ' - '
    
return  p;
}

itostr.txt
只有注册用户登录后才能发表评论。