# Decimal to Binary Converter

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-11-2006
peckitt99
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.
• 10-11-2006
Happy_Reaper
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.
• 10-11-2006
peckitt99
ah right cool, ill give it ago n come back if i got any probs, thanks
• 10-11-2006
SKeane
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); }```
• 10-11-2006
peckitt99
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?
• 10-11-2006
whiteflags
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"); }```
• 10-11-2006
Dave_Sinkula
Quote:

Originally Posted by peckitt99
is there anyother way to put it?
Code:

` unsigned int mask = 0x80000000;`

Yes.
• 10-12-2006
SKeane
Code:

`unsigned int mask = 1 << 31;`
• 10-12-2006
whiteflags
Not all unsigned integers are guaranteed to be 32 bits. It's common, but read the link Dave posted.
• 10-12-2006
SKeane
Whatever ...
• 10-12-2006
quzah
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.
• 10-12-2006
SKeane
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.
• 10-12-2006
quzah
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.
• 10-12-2006
SKeane
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.
• 10-12-2006
quzah
I'm not denying that you're insulted. I just don't care. Now you're way off topic.

Quzah.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last