Convert decimal to binary in C

2020-02-01 03:10发布

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 dectobin(int value, char* output)
{
    int i;
    output[5] = '\0';
    for (i = 4; i >= 0; --i, value >>= 1)
    {
        output[i] = (value & 1) + '0';
    }
}

Any help would be much appreciated!

标签: c binary decimal
16条回答
Ridiculous、
2楼-- · 2020-02-01 03:48
#include <stdio.h>
#include <stdlib.h>
void bin(int num) {
    int n = num;
    char *s = malloc(sizeof(int) * 8);
    int i, c = 0;
    printf("%d\n", num);

    for (i = sizeof(int) * 8 - 1; i >= 0; i--) {
        n = num >> i;
        *(s + c) = (n & 1) ? '1' : '0';
        c++;
    }
    *(s + c) = NULL;
    printf("%s", s); // or you can also return the string s and then free it whenever needed
}

int main(int argc, char *argv[]) {
    bin(atoi(argv[1]));
    return EXIT_SUCCESS;
}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-02-01 03:50

The value is not decimal. All values in computer's memory are binary.

What you are trying to do is to convert int to a string using specific base. There's a function for that, it's called itoa. http://www.cplusplus.com/reference/cstdlib/itoa/

查看更多
女痞
4楼-- · 2020-02-01 03:50

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
查看更多
相关推荐>>
5楼-- · 2020-02-01 03:54

You can do it using while loop under a function also. I was just searching the solve for mine but the solves i get were not suitable, so I have done it accordingly the practical approach (divide using 2 until getting 0 and store the reminder in an array) and print the reverse of the array and Shared Here

#include <stdio.h>

    int main()
    {
        long long int a,c;
        int i=0,count=0;
        char bol[10000];
        scanf("%lld", &a);
        c = a;
        while(a!=0)
        {
            bol[i] = a%2;
            a = a / 2;
            count++;
            i++;
        }
        if(c==0)
        {
            printf("0");
        }
        else
        {
            for(i=count-1; i>=0; i--)
            {
                printf("%d", bol[i]);
            }
        }
        printf("\n");
        return 0;
    }
查看更多
The star\"
6楼-- · 2020-02-01 03:54

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.

查看更多
兄弟一词,经得起流年.
7楼-- · 2020-02-01 03:55

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);
}
查看更多
登录 后发表回答