求用C语言将十六进制的字符数组转化为二进制的字符数组

2024-11-09 10:38:52
推荐回答(1个)
回答1:

试试这个
#include
#include
void main()
{
int i = 0;
char hexStr[] = "A57BF";
char binStr[256] = "";
for (; i {
switch(hexStr[i])
{
case '0':
strcat(binStr,"0000");
break;
case '1':
strcat(binStr,"0001");
break;
case '2':
strcat(binStr,"0010");
break;
case '3':
strcat(binStr,"0011");
break;
case '4':
strcat(binStr,"0100");
break;
case '5':
strcat(binStr,"0101");
break;
case '6':
strcat(binStr,"0110");
break;
case '7':
strcat(binStr,"0111");
break;
case '8':
strcat(binStr,"1000");
break;
case '9':
strcat(binStr,"1001");
break;
case 'A':
strcat(binStr,"1010");
break;
case 'B':
strcat(binStr,"1011");
break;
case 'C':
strcat(binStr,"1100");
break;
case 'D':
strcat(binStr,"1101");
break;
case 'E':
strcat(binStr,"1110");
break;
case 'F':
strcat(binStr,"1111");
break;
default:
printf("16进制数不合法\n");
return;
}
}
printf("%s\n",binStr);
}