二进制转换任意进制,有点问题 求助
程序代码:
#include<stdio.h> #include<string.h> int two_to_ten(char *p,int n) { int sum = 0; for(int i = 0;i<n;i++) { sum *= 2 ; sum += p[i] - '0'; } return sum; } void ten_to_any(int n, int change,char * buf) { char bbq[128] = {0}; int count =0 ; while(1) { bbq[count] = n % change ; count++; n=n/change; if(bbq[count] >= 0 && bbq[count] <= 9) { bbq[count] = bbq[count] +'0'; } else bbq[count] = bbq[count] + 'A'-10; if(n==0) break; } for(int i =0;i<count;i++) buf[i]=bbq[count-i-1]; } int main() { char buf[128]={0}; printf("Please input a two_:\n"); gets(buf); int n = two_to_ten(buf,strlen(buf)); printf("转换的十进制为%d\n",n); char buf1[128] = {0}; int x = 0; printf("请输入要转换的进制:\n"); scanf("%d",&x); ten_to_any(n,x,buf1); puts(buf1); return 0; }
这是代码 二进制先转换成10进制 10进制在求余数 取反, 十转任意会出现乱码, 求解?