1. ## Digital to Binary

I'm trying to figure out how to add the 0's in front of the binary out put.

ie: when I enter 23 I get 10111 I would like it to read 00010111 or 0001 0111

Code:
```#include <stdio.h>
#include <conio.h>

int main(void)
{
long b[20], n, r, c = 0, i ;
printf("Enter a decimal number: ");
scanf("%ld", &n) ;

if(n <= 0 || n > 255)
{
printf("Only numbers between 1 - 255");
return 1;
}
while(n > 0)
{
r = n % 2 ;
b[c] = r ;
n = n / 2 ;
c++ ;
}
printf("The binary equivalent is : ");

for(i = c - 1 ; i >= 0; i--)
{
printf("%ld", b[i]) ;
}
}```

2. Why not set all the values originally to 0 then make sure the for loop that does the print statement is setup in a different manner so it goes through 8 iterations

3. You need to replace your while loop with a for loop, the number of iterations matching the number of digits you want.

It is better to write a helper function to do the conversion. For example:
Code:
```/* Convert value to binary.
* buffer must have room for at least (digits + 1) chars.
*/
char *to_binary(char *const buffer, int digits, unsigned long value)
{
buffer[digits] = '\0';

while (digits-- > 0) {
buffer[digits] = '0' + (left & 1UL);
left /= 2UL;
}

return buffer;
}```
To use this, you can do for example
Code:
```int main(void)
{
char  string[10];
int   number;

number = 85;

printf("%d in decimal is %s in binary.\n", number, to_binary(string, 8, number));

return 0;
}```

4. Thanks

Originally Posted by Sorinx
Why not set all the values originally to 0 then make sure the for loop that does the print statement is setup in a different manner so it goes through 8 iterations

5. Thanks so much- This gives me something to go with.

6. With your example would I declare left as

int left; or left = 0;

It's undeclared

Thanks
Bob

Originally Posted by Nominal Animal
You need to replace your while loop with a for loop, the number of iterations matching the number of digits you want.

It is better to write a helper function to do the conversion. For example:
Code:
```/* Convert value to binary.
* buffer must have room for at least (digits + 1) chars.
*/
char *to_binary(char *const buffer, int digits, unsigned long value)
{
buffer[digits] = '\0';

while (digits-- > 0) {
buffer[digits] = '0' + (left & 1UL);
left /= 2UL;
}

return buffer;
}```
To use this, you can do for example
Code:
```int main(void)
{
char  string[10];
int   number;

number = 85;

printf("%d in decimal is %s in binary.\n", number, to_binary(string, 8, number));

return 0;
}```

7. Ouch. Copy-paste error. Replace left with value.

Code:
```/* Convert value to binary.
* buffer must have room for at least (digits + 1) chars.
*/
char *to_binary(char *const buffer, int digits, unsigned long value)
{
buffer[digits] = '\0';

while (digits-- > 0) {
buffer[digits] = '0' + (value & 1UL);
value /= 2UL;
}

return buffer;
}```

8. Ok thanks....

Originally Posted by Nominal Animal
Ouch. Copy-paste error. Replace left with value.

Code:
```/* Convert value to binary.
* buffer must have room for at least (digits + 1) chars.
*/
char *to_binary(char *const buffer, int digits, unsigned long value)
{
buffer[digits] = '\0';

while (digits-- > 0) {
buffer[digits] = '0' + (value & 1UL);
value /= 2UL;
}

return buffer;
}```