keil中用C语言16进制转二进制的代码
时间: 2023-08-06 20:03:01 浏览: 194
Keil-C.rar_keil转换
以下是用C语言实现16进制转换为二进制的代码:
```c
#include <stdio.h>
int main() {
char hex[17];
long long binary = 0;
int i = 0;
// 读入16进制数
printf("请输入16进制数:");
scanf("%s", hex);
// 转换为二进制
while (hex[i]) {
switch (hex[i]) {
case '0':
binary = binary * 16 + 0;
break;
case '1':
binary = binary * 16 + 1;
break;
case '2':
binary = binary * 16 + 10;
break;
case '3':
binary = binary * 16 + 11;
break;
case '4':
binary = binary * 16 + 100;
break;
case '5':
binary = binary * 16 + 101;
break;
case '6':
binary = binary * 16 + 110;
break;
case '7':
binary = binary * 16 + 111;
break;
case '8':
binary = binary * 16 + 1000;
break;
case '9':
binary = binary * 16 + 1001;
break;
case 'A':
case 'a':
binary = binary * 16 + 1010;
break;
case 'B':
case 'b':
binary = binary * 16 + 1011;
break;
case 'C':
case 'c':
binary = binary * 16 + 1100;
break;
case 'D':
case 'd':
binary = binary * 16 + 1101;
break;
case 'E':
case 'e':
binary = binary * 16 + 1110;
break;
case 'F':
case 'f':
binary = binary * 16 + 1111;
break;
default:
printf("输入的不是16进制数。\n");
return 0;
}
i++;
}
printf("二进制数为:%lld", binary);
return 0;
}
```
这个程序会要求用户输入一个16进制数,然后将其转换为二进制数,并输出结果。注意,这个程序只能处理一个16进制数,如果输入多个16进制数,只会处理第一个。
阅读全文