10进制转2进制

此代码不适合大数
方法是不断对2求模,求商

#include <stdio.h>
#include <string.h>
int main(){
char str[40];
while(scanf("%s",str)!=EOF){
int tmp=0;//代表数字10进制
int len=strlen(str);
int c=1,x;
int buf[1000],size=0;
for(int i=len-1;i>=0;i–){
x=str[i]-‘0’;//转换为数字数组
tmp=tmp+xc;
c=c
10;
}
while(tmp!=0){
buf[size++]=tmp%2;
tmp=tmp/2;
}
for(int i=size-1;i>=0;i–){
printf("%d",buf[i]);
}

}

}

猜你喜欢

转载自blog.csdn.net/ljh_mm/article/details/88541602