【问题标题】:not printing binary properly没有正确打印二进制文件
【发布时间】:2023-04-07 11:30:01
【问题描述】:

我正在为学校做一个项目,所以我不想找人为我做我的工作,但我想不通。我需要编写一个程序,将基数为 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 和 '1' 和 "1" 都非常不同
  • 1 是整数 1,'1' 是一个 char,其 ascii 值不是数字 1,而“1”是一个 指针 到一个常量 char 数组,它不能很好地进行比较

标签: c binary hex


【解决方案1】:

您应该使用if (hexNum[j] == '0'){ 而不是if (hexNum[j] = 0){。像这样的线条有很多。在您的程序中,您使用= 制作hexNum[j] value to 0。要进行比较,您需要使用==

在这种情况下,您应该在所有比较中使用字符表示法(如“0”而不是 0 或“0”)。 0、“0”和“0”是不同的。 0 是整数,'0' 是字符,"0" 是字符串。

这是工作代码。

#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;
}

【讨论】:

  • 所以我这样做了,更改了代码的所有必要部分。我什至在底部添加了一个打印语句,打印出 hexNum[2] 的值来测试以确保我得到了一个合适的字符。每当我运行程序并点击输入带有二进制数的字符串时,都不会打印出来。所以假设当 hexNum[2] == 6 出现时,字符串 0110 应该显示在终端中。但是当 char 值匹配时,不会打印 if else 块中的任何字符串
  • 好的,我正在为此添加更新。当我注释掉 for 循环中的 printf("%c", hexNum[j] 和 printf("\n\n") 行时(我相信第 35 和 36 行),代码将执行到二进制数所在的位置打印。现在我不明白为什么我似乎无法理解为什么十六进制值和二进制值都没有被打印出来。它只会显示一个或另一个。为了让它显示二进制字符串,我必须注释掉这些行打印十六进制值的代码
  • 想通了,我需要将 for 循环的内容放在括号之间,以便执行整个代码。
  • 我在上面的答案中编辑并添加了解决方案。尝试学习一些 C 概念,然后在发布此类问题之前进行一些研究。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2021-05-25
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多