unsigned long十进制转换为十六进制只用putchar输出

#include
#include


void ten_to_sixteen(long s)
{
int i,j;
long a,m,p,c,b[10];


a=m=s;

for( j=1; ;j++ )
{
p=m/16;
m=p;
if( p==0 )
{
break;
}
}
for( i=0;i {
c=a%16;
b[i]=c;
a=a/16;
}


printf( "转换的十六进制数结果:" );
putchar('0');
putchar('x');
j--;
for(;j>=0;j--)
{
switch(b[j])
{
case 0:
putchar('0');
break;
case 1:
putchar('1');
break;
case 2:
putchar('2');
break;
case 3:
putchar('3');
break;
case 4:
putchar('4');
break;
case 5:
putchar('5');
break;
case 6:
putchar('6');
break;
case 7:
putchar('7');
break;
case 8:
putchar('8');
break;
case 9:
putchar('9');
break;
case 10:
putchar('A');
break;
case 11:
putchar('B');
break;
case 12:
putchar('C');
break;
case 13:
putchar('D');
break;
case 14:
putchar('E');
break;
case 15:
putchar('F');
break;
default:
break;
}

}
}


int main()
{
long num;
printf( "请输入要转换的十进制数:" );
scanf("%ld",&num);
ten_to_sixteen(num);
printf( "\n" );
return 0;
}

你可能感兴趣的:(LINUX/C,include,c)