user-0 提问: 关于进制转换

回复:
user: user-1
content: 引用 15 楼 ForestDB 的回复:多一少一是常见的错误，多练习，无他。 “多一少一”问题占程序员常犯错误的10%以上！ 避免“多一少一”问题的方法之一是将比如<10甚至<5的数代入程序片断，搬手指头心算验证一下程序到底应该写为 x、x-1、x+1中的哪个？ <、<=、==、>、>=中的哪个？

user: user-2
content: 多一少一是常见的错误，多练习，无他。

user: user-0
content: 引用 1 楼 truelance 的回复:在while和for之间插一个i--;  一针见血，看到关键点了 你是怎么看出来的，怎么我写的时候没注意到，怎么才能避免这种错误？

user: user-3
content: 等会儿仔细看看楼主程序，也正在想写这个呢！

user: user-3
content: ltoa函数？

user: user-1
content: 仅供参考而已。

user: user-0
content: 引用 2 楼 zhao4zhong1 的回复:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <locale.h> int main() {     int i,v;     char bs[33];     char b[33];     char hs[9];     char h[9];     char s[4];     char *e;  // 十进制整数转二进制串；     i=1024;     ltoa(i,b,2);     sprintf(bs,"%032s",b);     printf("i=%d,bs=%s\n",i,bs); // 十进制整数转十六进制串；     i=1024;     ltoa(i,h,16);     sprintf(hs,"%08s",h);     printf("i=%d,hs=%s\n",i,hs); // 十六进制字符串转成十进制数     strcpy(hs,"00000400");     sscanf(hs,"%x",&i);     printf("hs=%s,i=%d\n",hs,i); // 二进制字符串转化为十六进制字符串；     strcpy(bs,"00000000000000000000010000000000");     i=strtol(bs,&e,2);     ltoa(i,h,16);     sprintf(hs,"%08s",h);     printf("bs=%s,hs=%s\n",bs,hs); // 二进制字符串转化为十进制数；     strcpy(bs,"00000000000000000000010000000000");     i=strtol(bs,&e,2);     printf("bs=%s,i=%d\n",bs,i); // 十六进制字符串转成二进制串     strcpy(hs,"00000400");     sscanf(hs,"%x",&i);     ltoa(i,b,2);     sprintf(bs,"%032s",b);     printf("hs=%s,bs=%s\n",hs,bs); // ASC\GBK字符串转十六进制串     strcpy(s,"a汉");     i=0;     while (1) {         if (0==s[i]) break;         sprintf(hs+i*2,"%02X",(unsigned char)s[i]);         i++;     }     setlocale(LC_ALL,"chs");     printf("s=%s,hs=%s\n",s,hs); // 十六进制字符串转成汉字（GBK）及字符(ASC)     strcpy(hs,"61BABA");     i=0;     while (1) {         if (1!=sscanf(hs+i*2,"%2x",&v)) break;         s[i]=(char)v;         i++;     }     s[i]=0;     printf("hs=%s,s=%s\n",hs,s);      return 0;  } //i=1024,bs=00000000000000000000010000000000 //i=1024,hs=00000400 //hs=00000400,i=1024 //bs=00000000000000000000010000000000,hs=00000400 //bs=00000000000000000000010000000000,i=1024 //hs=00000400,bs=00000000000000000000010000000000 //s=a汉,hs=61BABA //hs=61BABA,s=a汉   这样写不太好，我设想的是这个程序能够把输入的任意整数转化为任意进制的数，如把5转化为3进制，变得更智能。

user: user-0
content: 引用 3 楼 TBWood 的回复:i--   for(i--;i>=0;i--)         {                 printf("%d",a[i]);         } 这里for后面第一个i--不对，应该是i

user: user-4
content: #include <stdio.h> int main() {         long m,n,t,k;         printf("请输入您想要转换的十进制整数：\n");         scanf("%d",&m);         t=m;         printf("请输入您想要转换成的进制数：\n");         scanf("%d",&n);         char a[10000]={0};         int i=0;         while(m!=0)         {        k=m;                 m=m/n;                 a[i++]=k%n;         }         printf("%d的%d进制表示为：",t,n);        bool b = false;         for(i;i>=0;i--)         {                 if(a[i] == 0)                 {                     if(b == true) printf("%d",a[i]);                 }                else                {                 printf("%d",a[i]);                 b = true;                }         }         return 0; }

user: user-5
content: 限制输出位数，只输出后三位即可

user: user-6
content: 可能有些问题：putchar('0' + r); 比十进制大的不能显示a……

user: user-6
content: #include <stdio.h> void to_base_n(int x, int base); int main(void) {   int number;   int b;      printf("Enter an integer (q to quit):\n");   while (scanf("%d", &number) == 1)   {        printf("Enter number base (2-10): ");        scanf("%d", &b);      printf("Base %d equivalent: ", b);      to_base_n(number, b);      putchar('\n');      printf("Enter an integer (q to quit):\n");   }   return 0; } void to_base_n(int x, int base)   /* recursive function */ {   int r;    r = x % base;   if (x >= 2)      to_base_n(x / base, base);   putchar('0' + r);   return; }  用递归做的

user: user-7
content: 从最高位第一个不为0的开始输出。

user: user-8
content: i--   for(i--;i>=0;i--)         {                 printf("%d",a[i]);         }

user: user-1
content: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <locale.h> int main() {     int i,v;     char bs[33];     char b[33];     char hs[9];     char h[9];     char s[4];     char *e;  // 十进制整数转二进制串；     i=1024;     ltoa(i,b,2);     sprintf(bs,"%032s",b);     printf("i=%d,bs=%s\n",i,bs); // 十进制整数转十六进制串；     i=1024;     ltoa(i,h,16);     sprintf(hs,"%08s",h);     printf("i=%d,hs=%s\n",i,hs); // 十六进制字符串转成十进制数     strcpy(hs,"00000400");     sscanf(hs,"%x",&i);     printf("hs=%s,i=%d\n",hs,i); // 二进制字符串转化为十六进制字符串；     strcpy(bs,"00000000000000000000010000000000");     i=strtol(bs,&e,2);     ltoa(i,h,16);     sprintf(hs,"%08s",h);     printf("bs=%s,hs=%s\n",bs,hs); // 二进制字符串转化为十进制数；     strcpy(bs,"00000000000000000000010000000000");     i=strtol(bs,&e,2);     printf("bs=%s,i=%d\n",bs,i); // 十六进制字符串转成二进制串     strcpy(hs,"00000400");     sscanf(hs,"%x",&i);     ltoa(i,b,2);     sprintf(bs,"%032s",b);     printf("hs=%s,bs=%s\n",hs,bs); // ASC\GBK字符串转十六进制串     strcpy(s,"a汉");     i=0;     while (1) {         if (0==s[i]) break;         sprintf(hs+i*2,"%02X",(unsigned char)s[i]);         i++;     }     setlocale(LC_ALL,"chs");     printf("s=%s,hs=%s\n",s,hs); // 十六进制字符串转成汉字（GBK）及字符(ASC)     strcpy(hs,"61BABA");     i=0;     while (1) {         if (1!=sscanf(hs+i*2,"%2x",&v)) break;         s[i]=(char)v;         i++;     }     s[i]=0;     printf("hs=%s,s=%s\n",hs,s);      return 0;  } //i=1024,bs=00000000000000000000010000000000 //i=1024,hs=00000400 //hs=00000400,i=1024 //bs=00000000000000000000010000000000,hs=00000400 //bs=00000000000000000000010000000000,i=1024 //hs=00000400,bs=00000000000000000000010000000000 //s=a汉,hs=61BABA //hs=61BABA,s=a汉

user: user-9
content: 在while和for之间插一个i--;