- 论坛徽章:
- 0
|
关于十进制与十六进制的互相转换的问题?
看看我这些个函数对否:
int er_shiliu(char a[],int count,char s[])
{int number,j=0,sum=0;
char s2[36];
if (count==-2) return -2;
for (;count>;=0
{number=1;
while(number<9)
{if (a[count--]=='1')
sum=sum+number;
if (count==-1) break;
number*=2;}
if (sum>;9)
s2[j]=sum-10+'A';
else
s2[j]=sum+48;
j++;
sum=0;}
s2[j--]='\0';
while(1)
{if (j==0) break;
if (s2[j]=='0')
j--;
else
break;}
for (count=0;count<=j;count++)
s[count]=s2[j-count];
s[count]='\0';
return j;
}
//=============================
int shi_er(char x[],int count,char s[])
{int i,j=0,t;
unsigned long x_s=0;
char s2[130];
if (count==-2) return -2;
for (i=count;i>;=0;i--)
{t=x-48;
x_s+=t*pow(10,j);
j++;}
i=0;
while(1)
{s2=x_s%2;
x_s/=2;
i++;
if (x_s==0) break; }
s[0]='0';
s2[i--]='\0';
for (j=0;j<=i;j++)
s[j+1]=s2[i-j]+48;
s[j+1]='\0';
return j;
}
//=========================================
int shi_shiliu(char x[],int count,char s[])
{char s2[130];
int sc,s2c;
if (count==-2) return -2;
s2c=shi_er(x,count,s2);
sc=er_shiliu(s2,s2c,s);
return sc;
} |
|