8个回答

``````#include <iostream>
using namespace std;
char metachar[] = "0123456789abcdef";

void tobasen(int x, int n)
{
if (x > 0)
{
tobasen(x / n, n);
cout << metachar[x % n];
}
}

int main()
{
int x = 100;
for (int i = 2; i <= 16; i++)
{
cout << i << "进制";
tobasen(x, i);
cout << endl;
}
}
``````

2进制1100100
3进制10201
4进制1210
5进制400
6进制244
7进制202
8进制144
9进制121
10进制100
11进制91
12进制84
13进制79
14进制72
15进制6a
16进制64

int x = 0;
cout << "please enter a number in base 10" << endl;
cin >> x;

`````` #include <stdio.h>

int binaryNum[16];  //存放转换后得到的二进制码
int count=0;  //计数十进制整数被2除的次数
int oneCount=0;  //得到的二进制码中1的个数

void main(){
int num;
printf("输入一个十进制的整数：");
scanf("%d",&num);

while( (num/2) != 1 ){  //判断条件为：除以2之后的商不等于1
binaryNum[count] = num%2;  //取余后的数字存入数组
num /= 2;  //num = num/2; 进行下一轮的判断
count++;  //此变量用来指定数组下标
}
binaryNum[count+1] = 1;  //最后一次除法做完以后，剩下的商一定是1，所以在最后手动添加一个1

printf("二进制数值为：");

}

``````

``````
void print_2(int val2)
{
unsigned char *p = (unsigned char*)&val2 + 3; //从低位到高位,低端字节计算机
for(int k = 0; k <= 3; k++)
{
int val2 = *(p-k);
for (int i = 7; i >= 0; i--)
{
if(val2 & (1 << i))
printf("1");
else
printf("0");
}
printf(" ");
}
}

int main()
{
int a;
scanf("%d", &a);
printf("十六进制为 ： %x \n", a);
printf("二进制为 ：");
print_2(a);
}
``````
`````` int Change(char NumStr[],int num,int system);
int main()
{
int num,BitNum;
char NumStr[32];
scanf("%d",&num);
for(int i=2;i<17;i++)
{
BitNum=Change(NumStr,num,i);
for(;BitNum!=-1;BitNum--)
printf("%c",NumStr[BitNum]);
printf("\n");
}
return 0;
}
int Change(char NumStr[],int num,int system)
{
int i=0;
while(num != 0)
{
NumStr[i] = num%system;
if(NumStr[i]<10)
NumStr[i]+='0';
else
{
NumStr[i]+='A';
NumStr[i]-=10;
}
num/=system;
i++;
}
return i-1;

}
``````