没有正确打印二进制

时间:2016-07-14 17:57:55

标签: c binary hex

我正在为学校做一个项目,所以我不是在寻找有人为我做我的工作,但我无法弄清楚这一点。我需要编写一个程序,将基数为10的数字转换为二进制和十六进制。我无法弄清楚为什么我的代码没有打印出二进制数字。结果总是打印出0001而不占用整个输出。我很难解释这是诚实的,但继承人的代码。

#include<stdio.h>
#define MAX 1000

int main()
{
    long int baseTenNum,remainder,hexQuotient;
    int i=1,j,temp;
    char hexNum[MAX], binaryNum[MAX];

    printf("Enter any Base10 number: ");
    scanf("%ld",&baseTenNum);

    // quotient variable to convert to hexidecimal value
    hexQuotient = baseTenNum;

    // while loop to get hexidecimal value
    while(hexQuotient!=0)
    {
         temp = hexQuotient % 16;

      // Converts integer to character
      if( temp < 10)
           temp =temp + 48;
      else
         temp = temp + 55;

      hexNum[i++]= temp;
      hexQuotient = hexQuotient / 16;
    }



    printf("\nhexadecimal value of base 10 number %d: ",baseTenNum);
    for(j = i -1 ;j> 0;j--)
      printf("%c",hexNum[j]);
      printf("\n\n");

        if (hexNum[j] = 0){
            printf("0000");
        }
        else if(hexNum[j] = 1){
            printf("0001");
          }
        else if(hexNum[j] = 2 ){
            printf("0010");
        }
        else if(hexNum[j] = 3 )
        {
            printf("0011");
        }
        else if(hexNum[j] = 4 ){
            printf("0100");
        }
        else if(hexNum[j] = 5 ){
            printf("0101");
        }
        else if(hexNum[j] = 6){
            printf("0110");
        }
        else if(hexNum[j] = 7 ){
            printf("0111");
        }
        else if(hexNum[j] = 8 ){
            printf("1000");
        }
        else if(hexNum[j] = 9 ){
            printf("1001");
        } 
        else if(hexNum[j] = "A" ){
            printf("1010");
        }
        else if(hexNum[j] = "B" ){
            printf("1011");
        }
        else if(hexNum[j] = "C" ){
            printf(1100);
        }
        else if(hexNum[j] = "D" ){
            printf("1101");
        }
        else if(hexNum[j] = "E" ){
            printf("1110");
        }
        else if(hexNum[j] = "F" ){
            printf("1111");
        }
      printf("\n%c",hexNum[2]);


     return 0;


}

1 个答案:

答案 0 :(得分:3)

您应该使用if (hexNum[j] == '0'){代替if (hexNum[j] = 0){。并且有这么多行。在您的计划中,您使用hexNum[j] value to 0制作=。要比较您需要使用==

在这种情况下,您应该在所有比较中使用char符号(例如&#39; 0&#39;而不是0或&#34; 0&#34;)。 0,&#39; 0&#39;和&#34; 0&#34;是不同的。 0是整数,&#39; 0&#39;是一个角色,&#34; 0&#34;是一个字符串。

这是工作代码。

#include<stdio.h>
#define MAX 1000

int main()
{
    long int baseTenNum,remainder,hexQuotient;
    int i=0,j,temp;
    char hexNum[MAX], binaryNum[MAX];

    printf("Enter any Base10 number: ");
    scanf("%ld",&baseTenNum);

    // quotient variable to convert to hexidecimal value
    hexQuotient = baseTenNum;
    temp = baseTenNum;

    // while loop to get hexidecimal value
    while(hexQuotient != 0)
    {
        temp = hexQuotient % 16;
        // Converts integer to character
        if( temp <= 10)
            temp = temp + 48;
        else
            temp = temp + 55;

        hexNum[i++]= temp;
        hexQuotient = hexQuotient / 16;
    }


    printf("\nhexadecimal value of base 10 number %ld: ",baseTenNum);
    for(j = i-1 ;j >= 0;j--)

        if (hexNum[j] == '0'){
            printf("0000");
        }
        else if(hexNum[j] == '1'){
            printf("0001");
        }
        else if(hexNum[j] == '2' ){
            printf("0010");
        }
        else if(hexNum[j] == '3' )
        {
            printf("0011");
        }
        else if(hexNum[j] == '4' ){
            printf("0100");
        }
        else if(hexNum[j] == '5' ){
            printf("0101");
        }
        else if(hexNum[j] == '6'){
            printf("0110");
        }
        else if(hexNum[j] == '7' ){
            printf("0111");
        }
        else if(hexNum[j] == '8' ){
            printf("1000");
        }
        else if(hexNum[j] == '9' ){
            printf("1001");
        } 
        else if(hexNum[j] == 'A' ){
            printf("1010");
        }
        else if(hexNum[j] == 'B' ){
            printf("1011");
        }
        else if(hexNum[j] == 'C' ){
            printf("1100");
        }
        else if(hexNum[j] == 'D' ){
            printf("1101");
        }
        else if(hexNum[j] == 'E' ){
            printf("1110");
        }
        else if(hexNum[j] == 'F' ){
            printf("1111");
        }

    puts("");
    return 0;
}