C++十六进制转换二进制的代码
时间: 2024-06-12 15:09:50 浏览: 140
以下是将十六进制字符串转换为二进制字符串的 C 代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char hex[17];
char bin[65] = "";
int i = 0;
printf("请输入一个十六进制数:");
scanf("%s", hex);
while(hex[i]) {
switch(hex[i]) {
case '0':
strcat(bin, "0000");
break;
case '1':
strcat(bin, "0001");
break;
case '2':
strcat(bin, "0010");
break;
case '3':
strcat(bin, "0011");
break;
case '4':
strcat(bin, "0100");
break;
case '5':
strcat(bin, "0101");
break;
case '6':
strcat(bin, "0110");
break;
case '7':
strcat(bin, "0111");
break;
case '8':
strcat(bin, "1000");
break;
case '9':
strcat(bin, "1001");
break;
case 'a':
case 'A':
strcat(bin, "1010");
break;
case 'b':
case 'B':
strcat(bin, "1011");
break;
case 'c':
case 'C':
strcat(bin, "1100");
break;
case 'd':
case 'D':
strcat(bin, "1101");
break;
case 'e':
case 'E':
strcat(bin, "1110");
break;
case 'f':
case 'F':
strcat(bin, "1111");
break;
default:
printf("输入的不是十六进制数");
return 1;
}
i++;
}
printf("转换为二进制数为:%s", bin);
return 0;
}
```
阅读全文