问题遇到的现象和发生背景
字符型和整型的转换
中间的字符型和整型相互转换是怎么实现的啊?
其他进制转十进制中,讲字符型转换成整型:
int charToOct(char a[],int m) {
int product=1,sum=0;
int len=strlen(a);
for(int i=len-1; i>=0; i--) {
if(a[i]<='9') {
sum=sum+(a[i]-'0')*product; //字符型转换成整型
} else if(a[i]<='F') {
sum=sum+(a[i]-'A'+10)*product;
} else if(a[i]<='f') {
sum=sum+(a[i]-'a'+10)*product;
}
product=product*m;
}
return sum;
}
十进制转其他进制中将整形转换成字符型:
//b为输入的十进制数,m为进制,a存储
void octToChar(char a[],int m,int b) {
int i=0;
do {
int k=b%m;
if(k<=9) {
a[i++]='0'+k;//整型转换成字符型
} else {
a[i++]='A'+k-10;
}
b=b/m;
} while(b!=0);
a[i]='\n';
}