Digital to Binary

This is a discussion on Digital to Binary within the C Programming forums, part of the General Programming Boards category; I'm trying to figure out how to add the 0's in front of the binary out put. ie: when I ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    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

    Thanks for any help you can give me on this.

    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]) ;
        }
    }
    Last edited by brosskgm; 11-09-2012 at 08:40 PM.

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    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. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    847
    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. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Thanks


    Quote Originally Posted by Sorinx View Post
    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. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Thanks so much- This gives me something to go with.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    With your example would I declare left as

    int left; or left = 0;

    It's undeclared

    Thanks
    Bob


    Quote Originally Posted by Nominal Animal View Post
    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. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    847
    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. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Ok thanks....

    Quote Originally Posted by Nominal Animal View Post
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. digital watermark
    By not_so_cool in forum C Programming
    Replies: 1
    Last Post: 03-07-2011, 10:16 PM
  2. digital simulator
    By fuzon87 in forum C Programming
    Replies: 1
    Last Post: 03-29-2008, 01:06 PM
  3. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 12:05 PM
  4. Digital art
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-31-2003, 05:32 AM
  5. Digital mixing in DOS
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 01-11-2002, 07:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21