看看这个程序
将一个十进制数转化为2 8 16进制 不知道哪里错了 可以运行 但是结果不对#include<stdio.h>
#include<stdlib.h>
void trdec(char *str,int idec,int ibase)
{
int n,k;
char temp;
for(n=0,k=0;(idec/ibase)!=0;n++,k++)
{
if(idec%ibase<10)
{
str[n]='0'+idec%ibase;
}
else
{
str[n]='A'+idec%ibase-10;
}
idec/=ibase;
}
for(n=0;n<k/2;n++)
{
temp=str[n];
str[n]=str[k-1-n];
str[k-1-n]=temp;
}
str[k]='\0';
}
main()
{
FILE *fp;
char str[3][50],*p0=*str,*p1=str[1],*p2=str[2];
int idec;
scanf("%d",&idec);
if((fp=fopen("myf2.out","w"))==NULL)
{
printf("open fail\n");
exit(0);
}
trdec(*p0,idec,2);
printf("%s,",*p0);
fprintf(fp,"%s,",*p0);
trdec(p1,idec,8);
printf("%s,",p1);
fprintf(fp,"%s,",p1);
trdec(p2,idec,16);
printf("%s\n",p2);
fprintf(fp,"%s\n",p2);
fprintf(fp,"my text number is : 1231231234\n");
if(fclose(fp))
{
printf("close fail\n");
exit(0);
}
}