二进制数转换为对应的十进制、八进制和十六进制数,C语言?

2025-04-04 05:42:38
推荐回答(1个)
回答1:

#include
#include
char s[128];
int main(){
printf("Input a binary number:");
gets(s);
int len,i,n=0;
len=strlen(s);
for (i=0; i if(s[i]=='1'){
n<<=1;
n|=1;

}
else{
n<<=1;
}
}
printf("The number is %d in decimal.\n",n);
printf("The number is %o in octal.\n",n);
printf("The number is %X in octal.\n",n);
}