进制转换

代码
#include<stdio.h>
#include
<string.h>
const char bit[]={"0123456789ABCDEF"}; // 下表对应取余操作得出的值
void convert(char * s, int n ,int b)
{
if(0 == n)// 最后商为0时结束递归
{
strcpy( s,
"");
return ;
}
convert( s, n
/b, b);
int len = strlen(s);
s[len]
= bit[n%b];
s[len
+1] = '\0';
}
int main()
{
int n,b; //待转换数,转成的进制
char s[100]; //存转成后的数
while(scanf("%d%d",&n,&b)!=EOF)
{
if(0 == n)
{
printf(
"0\n");
continue;
}
convert( s, n, b);
printf(
"%s\n",s);
}
return 0;
}
原文地址:https://www.cnblogs.com/submarinex/p/1941251.html