Decimal to Binary Converter

This is a discussion on Decimal to Binary Converter within the C Programming forums, part of the General Programming Boards category; i am writing a function that will accept an integer as a parameter, convert it into a binary number (use ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Decimal to Binary Converter

    i am writing a function that will accept an integer as a parameter, convert it into a binary number (use a string to hold this) and return the binary string to the caller. The format of the binary number should be such that it is easy to read, i.e. split into blocks of 4. For example, the value 83247 would be output as a 32 bit number:

    0000 0000 0000 0001 0100 0101 0010 1111

    this is the code so far
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    int main()
    {
    	int input=0;
    	int count=0;
    	int binary_backwards[32];
    
    	printf("Enter a base 10 positive number to convert to binary:");
    	scanf("%i", &input);
    
    	while(input >0 && count<32)
    	{	
    		binary_backwards[count] = input %2;
    		input /=2;
    		count++;
    	}
    while ((--count) > -1) 
        printf("%d", binary_backwards[count]) ;
    	system("pause");
    }
    it works fine but i can see how to make it out put the extra zeros and put spaces after every 4.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    In your loop you can have another variable, say space_check, is incremented by one each iteration. When it is equal to 4, you print a space and resest it to 0. That takes care of the spaces.

    For the zeros, you should start your loop at 32 (dercrementing each iteration) and UNTIL it reaches count, you just print 0's.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    ah right cool, ill give it ago n come back if i got any probs, thanks

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int input=0;
        int i;
        unsigned int mask = 0x80000000;
    
        printf("Enter a base 10 positive number to convert to binary : ");
        scanf("%i", &input);
    
        for (i = 0; i < 32; i++)
        {
            if ( ((i % 4) == 0) && (i != 0) ) printf(" ");
            printf("%d", (input & mask) ? 1 : 0 );
            mask /= 2;
        }
    
        printf("\n");
    
        return(0);
    }

  5. #5
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah that code worked a bit better just a few more questions on is.

    what is this and is there anyother way to put it?
    Code:
     unsigned int mask = 0x80000000;
    and another one is i have tried to get it to also outbut the hexadecimal character for the number and also show the origonal number that was put in but for some reason i get some numbers for hex and the wrong number for the origonal number.

    this is my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main()
    {
       
     int input = 0;
     int i;
     unsigned int mask = 0x80000000; //how many bits to output in the correct way
    
     printf("Enter a Base 10 Number:  "); //user to enter number
     scanf("%i", &input); //input the number and start input as '0'
    
     system("cls");//clears the screen to show the outputs clearer
    
     printf ("Hex    :  %x,\n\n");
     printf("Binary :  ");
    
        for (i=0; i<32; i++) //increment 'i' if it is more than '0' but less that '32'
            {
            if ((i%4==0) && (i!=0)) //for ever 4 digits outputed by i put in a space
            printf(" "); //space
            printf("%d",(input&mask)?1:0); //print the binary number in 1's and 0's
            mask /=2; //put it in base 10
            }
                    
       printf("\n\n"); //new line      
       printf ("Base 10:  %d,\n\n"); //shows the base 10 number 
        
    system("pause");
    }
    can anyone help?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main()
    {
       
     int input = 0;
     int i;
     unsigned int mask = 0x80000000; //how many bits to output in the correct way
    The mask only ensures that we start with the highest bit.
    Code:
     printf("Enter a Base 10 Number:  "); //user to enter number
     scanf("%i", &input); //input the number and start input as '0'
    That comment is wrong.
    Code:
     system("cls");//clears the screen to show the outputs clearer
    
     printf ("Hex    :  %x,\n\n");
    I find it interesting that you aren't passing anything to printf().
    Code:
     printf("Binary :  ");
    
        for (i=0; i<32; i++) //increment 'i' if it is more than '0' but less that '32'
    No ......... Avoid comments that don't add information.
    Code:
            {
            if ((i%4==0) && (i!=0)) //for ever 4 digits outputed by i put in a space
            printf(" "); //space
            printf("%d",(input&mask)?1:0); //print the binary number in 1's and 0's
            mask /=2; //put it in base 10
    This comment is also wrong.
    Code:
            }
                    
       printf("\n\n"); //new line      
       printf ("Base 10:  %d,\n\n"); //shows the base 10 number
    Again you aren't passing anything to printf().
    Code:
        
       system("pause");
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by peckitt99
    is there anyother way to put it?
    Code:
     unsigned int mask = 0x80000000;
    Yes.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    unsigned int mask = 1 << 31;

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Not all unsigned integers are guaranteed to be 32 bits. It's common, but read the link Dave posted.

  10. #10
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Whatever ...

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SKeane
    Whatever ...
    You do know they've got 64 bit processors standard in most newer x86 PCs now, right? The 64-bit int called and said "Whatever...Dumbass!"

    Don't be so miffed because you were wrong. Learn and be wiser, or stop sniveling, and posting.

    I actually rather like being proven wrong regarding C, because it's such a rare occurance. But that's another topic.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    I was replying to a specific question,

    Q. Is there another way to put unsigned int mask = 0x80000000;

    A. unsigned int mask = 1 << 31;

    I didn't feel it need the extra caveat, as it had already been mentioned (in Dave's link). You obviously felt it did. No biggy, just a difference of opinion.

    I also didn't feel the need to insult fellow posters, or feel the need to brag.
    Last edited by SKeane; 10-12-2006 at 03:39 AM.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not bragging if it's true. Anyway, we like to be pedantic here. We also tend to write portable, accurate code. Most of us try not to assume things. The point is, you felt it wasn't needed, and you were wrong. Sucks to be you.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Again, with the insults (and denial). And, if we are going to be pedantic, just because something is true doesn't mean you can't brag about it.
    Last edited by SKeane; 10-12-2006 at 03:44 AM.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not denying that you're insulted. I just don't care. Now you're way off topic.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 07:35 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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