我没事写了个,你看看, 对于小的数字,这个程序根本就不用想, 所以我写了个大数字转换成16进制的: #include "Stdio.h" #include "Conio.h" char result[100]; int pos; void change(int *num); int main(void) { char str[100]; int num[100]; int bin[300]={0}; int quot[100],residue=0; int i,j,k=0,suffix=0,index=0; puts("Input a decimal number:"); gets(str); for(i=0;i<strlen(str);num[i]=str[i]-48,i++); j=i-1; while(j>=0) { num[j]%2?(bin[k++]=1):(bin[k++]=0); while(index<=j) { residue+=num[index]; if(residue>=2) quot[suffix++]=residue/2; else if(index) quot[suffix++]=0; residue=(num[index]%2)*10; index++; } for(i=0;i<suffix;num[i]=quot[i],i++); j=suffix-1; residue=suffix=index=0; } for(i=0;i<k;change(bin+i),i+=4); for(i=pos-1;i>=0;printf("%c",result[i]),i--); getch(); return 0; } void change(int *num) { if(num[3]==0&&num[2]==0&&num[1]==0&&num[0]==0) sprintf(result+pos,"%c",'0'); if(num[3]==0&&num[2]==0&&num[1]==0&&num[0]==1) sprintf(result+pos,"%c",'1'); if(num[3]==0&&num[2]==0&&num[1]==1&&num[0]==0) sprintf(result+pos,"%c",'2'); if(num[3]==0&&num[2]==0&&num[1]==1&&num[0]==1) sprintf(result+pos,"%c",'3'); if(num[3]==0&&num[2]==1&&num[1]==0&&num[0]==0) sprintf(result+pos,"%c",'4'); if(num[3]==0&&num[2]==1&&num[1]==0&&num[0]==1) sprintf(result+pos,"%c",'5'); if(num[3]==0&&num[2]==1&&num[1]==1&&num[0]==0) sprintf(result+pos,"%c",'6'); if(num[3]==0&&num[2]==1&&num[1]==1&&num[0]==1) sprintf(result+pos,"%c",'7'); if(num[3]==1&&num[2]==0&&num[1]==0&&num[0]==0) sprintf(result+pos,"%c",'8'); if(num[3]==1&&num[2]==0&&num[1]==0&&num[0]==1) sprintf(result+pos,"%c",'9'); if(num[3]==1&&num[2]==0&&num[1]==1&&num[0]==0) sprintf(result+pos,"%c",'A'); if(num[3]==1&&num[2]==0&&num[1]==1&&num[0]==1) sprintf(result+pos,"%c",'B'); if(num[3]==1&&num[2]==1&&num[1]==0&&num[0]==0) sprintf(result+pos,"%c",'C'); if(num[3]==1&&num[2]==1&&num[1]==0&&num[0]==1) sprintf(result+pos,"%c",'D'); if(num[3]==1&&num[2]==1&&num[1]==1&&num[0]==0) sprintf(result+pos,"%c",'E'); if(num[3]==1&&num[2]==1&&num[1]==1&&num[0]==1) sprintf(result+pos,"%c",'F'); pos++; } |