2014-12-13
展开全部
#include <stdio.h>
void trans10to2(int x)
{
int a[12]={0,0,0,0,0,0,0,0,0,0,0,0};//每位初始化为0
int i=0,rem;
do{
rem=x%2;
x=x/2;
a[i]=rem;
i++;
}while(x!=0);//这个算法看你的代码知道你懂!:)
printf("2进制:");
for (i=11;i>=0;i--) //倒输出正确顺序的数~
{
printf("%d",a[i]);
}
printf("\n");
}
//不带前缀 ,输出为12位数字,
void trans10to16(int x)
{
int a[12]={0,0,0,0,0,0,0,0,0,0,0,0};
int i=0,rem;
do
{
rem = x%16;
x = x/16;
a[i] = rem;
i++;
} while(x!=0);
printf("16进制:");
for (i=11;i>=0;i--) //倒输出正确顺序的数~
{
printf("%d",a[i]);
}
printf("\n");
}
//十六进制带前缀0x,输出为0x...
void trans10to16two(int x)
{
int a[10]={0,0,0,0,0,0,0,0,0,0};
int i=0,rem;
do
{
rem = x%16;
x = x/16;
a[i] = rem;
i++;
} while(x!=0);
printf("16进制:0x");
for (i=9;i>=0;i--) //倒输出正确顺序的数~
{
printf("%d",a[i]);
}
printf("\n");
}
void main()
{
int d;
printf("请输入一个十进制的正整数:\n");
do
{
scanf("%d",&d);
}
while(d<0);
trans10to2(d);
trans10to16(d);
trans10to16two(d);
}
输入
48
2进制: 000000110000
16进制:000000000030
16进制:0x0000000030
void trans10to2(int x)
{
int a[12]={0,0,0,0,0,0,0,0,0,0,0,0};//每位初始化为0
int i=0,rem;
do{
rem=x%2;
x=x/2;
a[i]=rem;
i++;
}while(x!=0);//这个算法看你的代码知道你懂!:)
printf("2进制:");
for (i=11;i>=0;i--) //倒输出正确顺序的数~
{
printf("%d",a[i]);
}
printf("\n");
}
//不带前缀 ,输出为12位数字,
void trans10to16(int x)
{
int a[12]={0,0,0,0,0,0,0,0,0,0,0,0};
int i=0,rem;
do
{
rem = x%16;
x = x/16;
a[i] = rem;
i++;
} while(x!=0);
printf("16进制:");
for (i=11;i>=0;i--) //倒输出正确顺序的数~
{
printf("%d",a[i]);
}
printf("\n");
}
//十六进制带前缀0x,输出为0x...
void trans10to16two(int x)
{
int a[10]={0,0,0,0,0,0,0,0,0,0};
int i=0,rem;
do
{
rem = x%16;
x = x/16;
a[i] = rem;
i++;
} while(x!=0);
printf("16进制:0x");
for (i=9;i>=0;i--) //倒输出正确顺序的数~
{
printf("%d",a[i]);
}
printf("\n");
}
void main()
{
int d;
printf("请输入一个十进制的正整数:\n");
do
{
scanf("%d",&d);
}
while(d<0);
trans10to2(d);
trans10to16(d);
trans10to16two(d);
}
输入
48
2进制: 000000110000
16进制:000000000030
16进制:0x0000000030
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询