您的位置:首页 > 其它

itoa()、atoi()、任意进制转换

2015-04-05 21:12 141 查看
头文件:<stdlib.h>

itoa --功能:将任意类型的数字转换为字符串。在<stdlib.h>中与之有相反功能的函数是atoi

atoi----功 能: 将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回(返回转换后的整型数)。
用 法: int atoi(const char *nptr);

代码1:itoa 实现任意进制的转换(整形-->字符串)

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main(){
int number=15;
char string[25];
itoa(number,string,4);
printf("integer=%d string=%s\n",number,string);
itoa(number,string,2);
printf("integer=%d string=%s\n",number,string);
itoa(number,string,8);
printf("integer=%d string=%s\n",number,string);
itoa(number,string,10);
printf("integer=%d string=%s\n",number,string);
itoa(number,string,16);
printf("integer=%d string=%s\n",number,string);
return 0;
}




代码2:atoi 字符串数字转化为整形数字(字符串-->整型)

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main(){
int number=16;
char string[25];
float n;
char str[]="12345.67";
n=atoi(str);
printf("string=%s  float=%f\n",str,n);
return 0;
}




#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main(){
char a[]="-100";
char b[]="123";
int c;
c=atoi(a)+atoi(b);
printf("c=%d\n",c);
return 0;
}




记住一点:itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。

是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似。

代码3:虽然可能itoa无法使用,但是我们可以编写自己的itoa()函数,以下是实现源代码(来源网络):

#include<iostream>
#include<stdio.h>
using namespace std;

char*my_itoa(int num,char*str,int radix){//原数字,存放地址,要转换的转换进制
const char table[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
char*ptr=str ;
bool negative=false ;
if(num==0){
//num=0
*ptr++='0' ;
*ptr='/0' ;
// don`t forget the end of the string is '/0'!!!!!!!!!
return str ;
}
if(num<0){
//if num is negative ,the add '-'and change num to positive
*ptr++='-' ;
num*=-1 ;
negative=true ;
}
while(num){
*ptr++=table[num%radix];
num/=radix ;
}
*ptr='\0' ;
//if num is negative ,the add '-'and change num to positive
// in the below, we have to converse the string
char*start=(negative?str+1:str);
//now start points the head of the string
ptr--;
//now prt points the end of the string
while(start<ptr){
char temp=*start ;
*start=*ptr ;
*ptr=temp ;
start++;
ptr--;
}
return str ;
}
int main(){
int a=15;
char str[100];
my_itoa(a,str,8);
printf("%s\n",str);
return 0;
}


代码4:任意进制间的转换 (在任意进制之间进行转换,通过十进制中介。)

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;

long toTen(char a[],int bit){//任意进制到10进制。(a是要转换的数,bit是原本的进制(2~36)----要用数组存储要转换的数字,结果返回整型的十进制数)
long i,b=1,sum=0;
int length=strlen(a);
for (i=length-1;i>=0;i--){
if (a[i]>='A'){
sum+=(a[i]-'A'+10)*b;
b*=bit;
}
else{
sum+=(a[i]-'0')*b;
b*=bit;
}
}
return sum;
}
int main(){
int aNum;
char bNum[20];
//以整型读入,转换字符串带入函数,进行进制转换
cin>>aNum;
sprintf(bNum,"%d",aNum);
cout<<toTen(bNum,8)<<endl;      //假设原本是8进制,代入函数后返回10进制的数

//以字符串读入,直接代入函数,进行进制转换
cin>>bNum;
cout<<toTen(bNum,2)<<endl;      //假设原本是2进制

//把二进制10110转换为十六进制
aNum=toTen("1111",2);
itoa(aNum,bNum,16);
cout<<bNum<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: