Convert decimal to binary in C

后端 未结 16 693
攒了一身酷
攒了一身酷 2020-12-10 09:38

I am trying to convert a decimal to binary such as 192 to 11000000. I just need some simple code to do this but the code I have so far doesn\'t work:

void de         


        
相关标签:
16条回答
  • 2020-12-10 10:13

    Perhaps understanding the algorithm would allow you write or modify your own code to suit what you need. I do see that you don't have enough char array length to display your binary value for 192 though (You need 8 digits of binary, but your code only gives 5 binary digits)

    Here's a page that clearly explains the algorithm.

    I'm not a C/C++ programmer so here's my C# code contribution based on the algorithm example.

    int I = 0;
    int Q = 95;
    string B = "";
    while (Q != 0)
    {
        Debug.Print(I.ToString());
        B += (Q%2);
        Q = Q/2;
        Debug.Print(Q.ToString());
        I++;
    }
    
    Debug.Print(B);
    

    All the Debug.Print is just to show the output.

    0 讨论(0)
  • 2020-12-10 10:13
    //decimal to binary converter
    long int dec2bin(unsigned int decimal_number){
      if (decimal_number == 0)
        return 0;
      else
        return ((decimal_number%2) + 10 * dec2bin(decimal_number/2));
    }
    
    0 讨论(0)
  • 2020-12-10 10:15

    So... did you check the output of your code to understand why it doesn't work?

    So iteration 1 of your loop:
    value = 192
    i = 4
    output[i] = (11000000 & 1) + '0' = 0 + 48 = 48 (char `0`)
    
    Iteration 2 of your loop:
    value = 96
    i = 3
    output[i] = (1100000 & 1) + '0' = 0 + 48 = 48 (char `0`)
    
    Iteration 3 of your loop:
    value = 48
    i = 2
    output[i] = (110000 & 1) + '0' = 0 + 48 = 48 (char `0`)
    
    Iteration 4 of your loop:
    value = 24
    i = 1
    output[i] = (11000 & 1) + '0' = 0 + 48 = 48 (char `0`)
    
    Iteration 5 of your loop:
    value = 12
    i = 0
    output[i] = (1100 & 1) + '0' = 0 + 48 = 48 (char `0`)
    
    Final string: "00000"  and you wanted: "11000000"
    

    See anything wrong with your code? Nope. Neither do I you just didn't go far enough. Change your output/loop to:

    output[8] = '\0';
    for (i = 7; i >= 0; --i, value >>= 1)
    

    And then you'll have the correct result returned.

    I would recomend just a more general approach, you're using a fixed length string, which limits you to binary numbers of a certian length. You might want to do something like:

    loop while number dividing down is > 0
    count number of times we loop
    malloc an array the correct length and be returned
    
    0 讨论(0)
  • 2020-12-10 10:17

    5 digits are not enough for your example (192). Probably you should increase output

    0 讨论(0)
  • 2020-12-10 10:18

    Convert Decimal to Binary in C Language

    #include<stdio.h>
    void main()
    {
        long int n,n1,m=1,rem,ans=0;
        printf("\nEnter Your Decimal No (between 0 to 1023) :: ");
        scanf("%ld",&n);
    
        n1=n;
        while(n>0)
        {
            rem=n%2;
            ans=(rem*m)+ans;
            n=n/2;
            m=m*10;
        }
    
        printf("\nYour Decimal No is   :: %ld",n1);
        printf("\nConvert into Binary No is :: %ld",ans);
    }
    
    0 讨论(0)
  • 2020-12-10 10:18

    This is the simplest way to do it

    #include <stdio.h>
    
    void main()
    {
        int n,i,j,sum=0;
        printf("Enter a Decimal number to convert it to binary : ");
        scanf("%d",&n);
        for(i=n,j=1;i>=1;j*=10,i/=2)
            sum+=(i%2)*j;
        printf("\n%d",sum);
    }
    
    0 讨论(0)
提交回复
热议问题