itoa函数的实现(不同进制)
2013-07-08 17:12:30
itoa函数相对于atoi函数,比较简单,还是要注意考虑的全面。
小结:
一下几点需要考虑:
- 对负数,要加上负号;
- 考虑不同进制,根据要求进行处理;对不同的进制转换方法不同(如对于16进制还要考虑10~15的情况:*str++ = digit[cnt--] - 10 + 'A');
- 最后要加上字符串结束符*str = '\0';
- 要正确输出绝对值最大的负数,就必须将输入转换为无符号数,因为对下面的代码:
long int num = -2147483648;
cout<<-num<<endl;
执行结果为:-2147483648
因为-2147483648取负号,为2147483648,但对于32位的long int型,其所能表示的最大正数为2147483647,因此2147483648将转换为-2147483648,在执行下面的while循环时就会得到意想不到的怪异结果。
对16进制,要加上前缀:
if (false == PreFlag)
{
*str++ = '0';
*str++ = 'x';
PreFlag = true;
}
最8进制,同样:
if (false == PreFlag)
{
*str++ = '0';
PreFlag = true;
}
下面代码中的数组digit还可以进行优化,如类型改为char型,可以节省空间。
代码:
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 5 #define SIZE 100 6 7 bool BaseError = false; 8 9 //考虑不同进制的转换 10 char * _itoa_1 (long int num,char str[],int base) 11 { 12 if ( !(base == 10 || base == 16 || base == 8) ) 13 { 14 BaseError = true; 15 return NULL; 16 } 17 18 int digit[20]; //保存各个数字 19 int cnt = 0; 20 int sign = 1; 21 bool PreFlag = false; 22 char *str_copy = str; 23 unsigned long int uNum = 0; 24 25 if (num < 0) //对负数的处理 26 { 27 sign = -1; 28 uNum = (unsigned long int) - num; //转换为unsigned long int,这样才能处理绝对值最大的负数 29 } 30 else 31 { 32 uNum = num; 33 } 34 35 while (uNum) 36 { 37 digit[cnt++] = uNum % base; 38 uNum = uNum / base; 39 } 40 41 --cnt; 42 43 if (cnt >= 0 && -1 == sign) //对负数,要加负号 44 { 45 *str++ = '-'; 46 } 47 48 while (cnt >= 0) 49 { 50 if (10 == base) //对不同的进制,转换方式不同 51 { 52 *str++ = digit[cnt--] + '0'; 53 } 54 else if (16 == base) 55 { 56 if (false == PreFlag) 57 { 58 *str++ = '0'; 59 *str++ = 'x'; 60 PreFlag = true; 61 } 62 else 63 { 64 if (digit[cnt] >= 0 && digit[cnt] <= 9) 65 { 66 *str++ = digit[cnt--] + '0'; 67 } 68 else 69 { 70 *str++ = digit[cnt--] - 10 + 'A'; 71 } 72 } 73 } 74 else if (8 == base) 75 { 76 if (false == PreFlag) 77 { 78 *str++ = '0'; 79 PreFlag = true; 80 } 81 else 82 { 83 *str++ = digit[cnt--] + '0'; 84 } 85 } 86 } 87 88 *str = '\0'; 89 return str_copy; //返回指针地址 90 } 91 92 //测试程序 93 int main() 94 { 95 char str[SIZE]; 96 int num = 0; 97 int base = 0; 98 cout<<"test _itoa_1..."<<endl; 99 cout<<"please enter the integer number and the base:"<<endl; 100 while(cin>>num>>base) 101 { 102 cout<<"the integer number is :"<<num<<endl; 103 cout<<"the int number is : "<<_itoa_1(num,str,base)<<endl; 104 cout<<"please enter the string :"<<endl; 105 cout<<"please enter the integer number and the base:"<<endl; 106 } 107 108 return 0; 109 }
运行结果:
test _itoa_1... please enter the integer number and the base: 2147364748 10 the integer number is :2147364748 the int number is : 2147364748 please enter the string : please enter the integer number and the base: -2147364749 10 the integer number is :-2147364749 the int number is : -2147364749 please enter the string : please enter the integer number and the base: 8934 16 the integer number is :8934 the int number is : 0x22E6 please enter the string : please enter the integer number and the base: 78 8 the integer number is :78 the int number is : 0116 please enter the string : please enter the integer number and the base: -78 16 the integer number is :-78 the int number is : -0x4E please enter the string : please enter the integer number and the base: -78 8 the integer number is :-78 the int number is : -0116 please enter the string : please enter the integer number and the base: ^Z 请按任意键继续. . .