嵌入式学习——C语言学习:
从键盘输入一个数,转化成二进制、十进制、十六进输出:
#include <stdio.h>
int main()
{
int str[100];
int a,i,j=0;
void two(int);
void ten(int);
void sixteen(int);
printf("Input a number:");
scanf("%d",&a);
two(a);
ten(a);
sixteen(a);
}
void two(int m)
{
int i = 0;
int two[100];
while(m != 0)
{
two[i]=m%2;
m /= 2;
i++;
}
printf("two num=");
for(i=i-1;i>=0;i--)
{
printf("%d",two[i]);
if(i%4 == 0)
{
printf(" ");
}
}
printf("\n");
}
void ten(int m)
{
int i = 0;
int ten[100];
while(m != 0)
{
ten[i]=m % 10;
m /= 10;
i++;
}
printf("ten num=");
for(i=i-1;i>=0;i--)
{
printf("%d",ten[i]);
}
printf("\n");
}
void sixteen(int m)
{
int i = 0;
char sixteen[100];
while(m != 0)
{
switch(m%16)
{
case 0:sixteen[i] = 'o';break;
case 1:sixteen[i] = '1';break;
case 2:sixteen[i] = '2';break;
case 3:sixteen[i] = '3';break;
case 4:sixteen[i] = '4';break;
case 5:sixteen[i] = '5';break;
case 6:sixteen[i] = '6';break;
case 7:sixteen[i] = '7';break;
case 8:sixteen[i] = '8';break;
case 9:sixteen[i] = '9';break:
case 10:sixteen[i] = 'A';break;
case 11:sixteen[i] = 'B';break;
case 12:sixteen[i] = 'C';break;
case 13:sixteen[i] = 'D';break;
case 14:sixteen[i] = 'E';break;
case 15:sixteen[i] = 'F';break;
}
m /= 16;
i++;
}
printf("sixteen num:");
for(i=i-1;i>=0;i--)
{
printf("%c",sixteen[i]);
}
printf("\n");
}