如题所述
以下代码能把十进制转换为2~16进制,再多了你可以照猫画虎……
//#include "stdafx.h"//vc++6.0加上这一行.#include "stdio.h"
#include "string.h"
void Itob(int n,char s[],int b){
char cx,i;
for(i=0;;i++){
cx=n%b;
s[i]=cx+(cx>=0 && cx<=9 ? '0' : 0x37);
n/=b;
if(n==0 && cx==0) break;
}
s[i]=NULL;
strcat(s,b==2 ? ")B(" : b==8 ? "O" : b==16 ? "x0" : "");
strrev(s);
}
int main(void){
char p[20];
int n,b;
printf("请输入一个十进制整数...\nn=");
scanf("%d",&n);
printf("请输入要转换成的进制数(2~16)...\nb=");
scanf("%d",&b);
Itob(n,p,b);
printf("输入的十进制数是 %d.\n转换成%d进制是 %s.\n",n,b,p);
return 0;
}
温馨提示:答案为网友推荐,仅供参考