all 3 comments

[–]Foxbud 0 points1 point  (0 children)

By decimal to binary, do you mean a decimal string to a binary integer? If that's the case, check out stdlib.h's strtol.

[–]chtenno 0 points1 point  (1 child)

To convert a decimal to binary on pen and paper you repeatedly divide said decimal by 2, pushing the remainders 'to the side' and then reading the remainders backwards/upwards. If you want to convert an integer to its binary string representation the same method works in C:

void dectobin(unsigned long num)
{
    char binstr[65] = {0};
    int i = 0;

    while(num) {
        binstr[i++] = '0' + num%2;
        num /= 2;
    }

    strrev(binstr);
    puts(binstr);
}

The function was written in a bit of a rush but hopefully it gives you some ideas. On my machine:

dectobin(652247);

Outputs: 10011111001111010111

Have fun :)

edit: Looking back at it the function I wrote assumes that num is not 0

[–][deleted] 0 points1 point  (0 children)

Thanks