从十六进制转换为二进制

时间:2014-09-03 10:44:40

标签: c

我试图用十六进制字符串显示二进制字符串。我的代码是

#include <stdio.h>
int main() {
    char hexa[5], num[120];
    int i = 0, j, k, t;
    char ch;
    printf("enetr hexadecimal");
    while (ch = getchar() != '\n') {
        scanf("%c", &ch);
        hexa[i] = ch;
        i++;
    }
    k = 0;
    for (j = 0; hexa[j] != '\0'; j++) {
        if (hexa[j] == 'A') {
            num[k++] = '1';
            num[k++] = '0';
            num[k++] = '1';
            num[k++] = '0';
        } else if (hexa[j] == 'B') {
            num[k++] = '1';
            num[k++] = '0';
            num[k++] = '1';
            num[k++] = '1';
        } else if (hexa[j] == 'C') {
            num[k++] = '1';
            num[k++] = '1';
            num[k++] = '0';
            num[k++] = '0';
        } else if (hexa[j] == 'D') {
            num[k++] = '1';
            num[k++] = '1';
            num[k++] = '0';
            num[k++] = '1';
        } else if (hexa[j] == 'E') {
            num[k++] = '1';
            num[k++] = '1';
            num[k++] = '1';
            num[k++] = '0';
        } else if (hexa[j] == 'F') {
            num[k++] = '1';
            num[k++] = '1';
            num[k++] = '1';
            num[k++] = '1';
        } else if (hexa[j] == '0') {
            num[k++] = '0';
            num[k++] = '0';
            num[k++] = '0';
            num[k++] = '0';
        } else if (hexa[j] == '1') {
            num[k++] = '0';
            num[k++] = '0';
            num[k++] = '0';
            num[k++] = '1';
        } else if (hexa[j] == '2') {
            num[k++] = '0';
            num[k++] = '0';
            num[k++] = '1';
            num[k++] = '0';
        } else if (hexa[j] == '3') {
            num[k++] = '0';
            num[k++] = '0';
            num[k++] = '1';
            num[k++] = '1';
        } else if (hexa[j] == '4') {
            num[k++] = '0';
            num[k++] = '1';
            num[k++] = '0';
            num[k++] = '0';
        } else if (hexa[j] == '5') {
            num[k++] = '0';
            num[k++] = '1';
            num[k++] = '0';
            num[k++] = '1';
        } else if (hexa[j] == '6') {
            num[k++] = '0';
            num[k++] = '1';
            num[k++] = '1';
            num[k++] = '0';
        } else if (hexa[j] == '7') {
            num[k++] = '0';
            num[k++] = '1';
            num[k++] = '1';
            num[k++] = '1';
        } else if (hexa[j] == '8') {
            num[k++] = '1';
            num[k++] = '0';
            num[k++] = '0';
            num[k++] = '0';
        } else if (hexa[j] == '9') {
            num[k++] = '1';
            num[k++] = '0';
            num[k++] = '0';
            num[k++] = '1';
        }
    }
    for (t = 0; num[t] != '\0'; t++)
        printf("%c", num[t]);
    return 0;
}

此代码段仅显示十六进制字符串的第一个字符而不是整个字符串。请帮助解决导致错误的原因并帮助我纠正错误

2 个答案:

答案 0 :(得分:3)

您(出于某种原因)将此处理为文本,这比将其作为实际数字处理要复杂得多。

你可以通过以下方式逃脱:

unsigned int hexa;

if(scanf(" %x", &hexa) == 1)
{
  int i;
  for(i = (CHAR_BIT * sizeof hexa) - 1; i >= 0; --i)
  {
    putchar('0' + ((hexa & (1u << i)) != 0));
  } 
  putchar('\n');
}

以上将始终生成二进制的固定(可能是32位)宽度,如果您真的需要可变长度的二进制数,则可以采用不同的方式。

答案 1 :(得分:0)

这对你有用 -

# include <stdio.h>
int main()
{
        unsigned int num;
        int i, j;

        printf("Enter an Hex number\n");
        if(scanf(" %x", &num)!=1){
        printf("Enter proper Hexadecimal number\n");
        return 0;
        }
        printf("%x in binary format is- \n", num);
        for (i = 31; i >= 0; i--)
        {
                if ((num>>i)&1)
                        printf("1");
                else
                        printf ("0");
        }
        printf("\n");
        return 0;
}