#include <stdio.h>
int changebase(int number, int base);
int bitchangeradix(int i , int radix);
int main() {
int number=0;
scanf("%d",&number);
printf("%o %x\n", number, number);
// printf("%d %d\n", bitchangeradix(number,8), bitchangeradix(number,16));
printf("%d %d", changebase(number,8),changebase(number, 16));
return 0;
}
//changebase函数当number为负数时陷入死循环
int changebase(int number, int base) {
int a = 0;
int i = 1;
while (number) {
int tmp = number % base;
number /= base;
tmp *= i;
i *= 10;
a += tmp;
}
return a;
}
int bitchangeradix(int i , int radix){
int t ,step;
if(radix==8)
t=7,step=3;
else if (radix==16)
t=15,step=4;
else
return -1;
int tmp=0;
int ten=1;
while (i){
tmp+=(i&t)*ten;
ten*=10;
i=i>>step;
}
return tmp;
}
图为printf格式化输出和bitchangeradix的结果
bitchangeradix函数思路是讲二进制串 按照3个一组(八进制)看成一个十进制数来读