试试这个。采用的算法是权值法呦。新颖。
#include
int dtob(int x, int b[15] );
main()
{
int i,n,b[15];
printf("\ninput to D:(0 -- 65535)");
scanf("%d",&n);
dtob(n,b);
/* for(i=15;i>=0;i--)
printf("\n");
printf("%d ",b[i]); */
}
/************************************/
int dtob(int x, int b[15] )
{
unsigned int bn[]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
char c[]={'0'迅念汪,'1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'},o[5]={'0'},h[]={'0','0','0','0'};
int n8,n16,f8,f16,i,j,k,n,no;
n8=n16=0;f8=f16=0;
/*int i,j,k,n,no;*/
no=n=x;
j=k=0;
for(i=15;i>=0;i--)
{
if(n>=bn[i]) {b[i]=1;n=n-bn[i];}
else b[i]=0;
/亩仔* printf("%d ",b[i]) ;*/
}
for(i=0;i<=15;i++)
{
f8=f8+b[i]*bn[n8];
printf("(%d) %d =%c= ",n8,f8,c[f8]);
if (n8>1)
{
j++;o[j]=c[f8];
n8=-1;f8=0;
}
n8++;
f16=f16+b[i]*bn[n16];
/* h[k]=c[f16]; */
printf("(%d) %d =%c= ",n16,f16,c[f16]);
if (n16>2)
{
k++;h[k]=c[f16];
n16=-1;f16=0;
}
n16++;
}
printf("\n\nD TO B %d --> ",no);
for(i=15;i>=0;i--)
printf("%d "高弊,b[i]);
printf("\n\n8--- %c%c%c%c%c",o[5],o[4],o[3],o[2],o[1]);
printf("\n\n16-- %c%c%c%c",h[4],h[3],h[2],h[1]);
}
//10->2
CString DecimalToBin(int iDecimal)
{
char c[20];
_itoa(iDecimal, c, 2 );
CString str(c);
return str;
}
//10->16
CString DecimalToHex(int iDecimal)
{
char c[20];
sprintf(c, "兆李陆%x"扰亏, iDecimal);/族顷/ _ltoa( l, buffer, 16 );
CString str(c);
return str;
}
//10->8
CString DecimalToOct(int iDecimal)
{
char c[20];
sprintf(c, "%o", iDecimal);
CString str(c);
return str;
}