# decimal to binary conversion

This is a discussion on decimal to binary conversion within the C Programming forums, part of the General Programming Boards category; Hello, I saw it. I have now this Code: /* * * ===================================================================================== * * Filename: test2.c * * Description: ...

1. Hello,

I saw it.
I have now this
Code:
```/*
*  * =====================================================================================
*
*          Filename:  test2.c
*
*          Description:  conventer from dec to 0101
*
*          Version:  1.0
*          Created:  14-05-11 10:03:25
*          Revision:  none
*          Compiler:  gcc
*
*          Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
*          Company:  FH Südwestfalen, Iserlohn
*
* =====================================================================================
*/

#include        <stdio.h>
#include        <stdlib.h>

int
main ( int argc, char *argv[] )
{
int nummer, uitkomst, teller, teller2;
char uitkomst2[10];
nummer = 4 ;
uitkomst = 4 ;
teller = 0 ;
while (uitkomst > 0)
{
uitkomst = nummer / 2 ;
uitkomst2[teller++]= uitkomst%2;
nummer = uitkomst;
printf ("teller:%d  deling:%d uitkomst deling:%d  \n",teller, uitkomst%2, uitkomst);
}
teller2 = 1 ;
while (teller2 < teller)
{
printf("%d", uitkomst2[teller2]);
teller2 = teller2 + 1 ;
}
return EXIT_SUCCESS;
}
/*c----------  end of function main  ---------- */```
It working now only not good.
4 gets now the answer 10 where it must be 100

Roelof

2. That's because you divide by 2 before trying to convert the number (as the first line in your while loop). Don't do that.

3. Originally Posted by tabstop
That's because you divide by 2 before trying to convert the number (as the first line in your while loop). Don't do that.
Old joke....

A guy walks into the doctor's office and hooks his thumb over the edge of the desk.
He pulls out a claw hammer and whacks himself right on the thumb.

"Doc, it hurts like heck every time I do that"

The doctor replied: "Then don't do that!".

4. Oke,

I changed it to this:
Code:
```/*
*  * =====================================================================================
*
*          Filename:  test2.c
*
*          Description:  conventer from dec to 0101
*
*          Version:  1.0
*          Created:  14-05-11 10:03:25
*          Revision:  none
*          Compiler:  gcc
*
*          Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
*          Company:  FH Südwestfalen, Iserlohn
*
* =====================================================================================
*/

#include        <stdio.h>
#include        <stdlib.h>

int
main ( int argc, char *argv[] )
{
int nummer, uitkomst, teller;
char uitkomst2[10];
nummer = 2 ;
uitkomst = 2 ;
teller = 0 ;
while (uitkomst > 0)
{
uitkomst2[teller++]= uitkomst%2;
uitkomst = nummer /2 ;
nummer = uitkomst;
printf ("teller:%d  deling:%d uitkomst deling:%d  \n",teller, uitkomst%2, uitkomst);
}
printf ("teller is nu %d", teller);
while (teller >  0)
{
printf("%d", uitkomst2[teller]);
teller = teller - 1;
}
return EXIT_SUCCESS;
}
/*c----------  end of function main  ---------- */```
But i see something wierd.
The output is now this
Code:
```teller:1  deling:1 uitkomst deling:1
teller:2  deling:0 uitkomst deling:0
teller is nu 241```
So the loop is executed right but after that teller is a complete other number.

Roelof

5. That's because you forgot to print a new line after the number. teller is 2, then you print some uitkomst2 after it.

6. A bit more simplification...
Code:
```            while (uitkomst > 0)
{
uitkomst2[teller++]= uitkomst%2;
uitkomst = nummer /2 ;
nummer = uitkomst;
printf ("teller:%d  deling:%d uitkomst deling:%d  \n",teller, uitkomst%2, uitkomst);
}```
You can eliminate the intermediate variable uitkomst, which you are only using to flip values in nummer by operating directly on nummer, like this...
Code:
```while (nummer > 0)
{ uitkomst2[teller++] = nummer %2;
nummer /= 2 ; }```
Again... you should always try to find the simplest solutions to a problem. I appreciate that this is all new to you, but the best time to form good habits is *before* you form the bad ones...

7. Hello,

I found this answer which is working :
Code:
```/*
*  * =====================================================================================
*
*          Filename:  test2.c
*
*          Description:  conventer from dec to 0101
*
*          Version:  1.0
*          Created:  14-05-11 10:03:25
*          Revision:  none
*          Compiler:  gcc
*
*          Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
*          Company:  FH Südwestfalen, Iserlohn
*
* =====================================================================================
*/

#include        <stdio.h>
#include        <stdlib.h>

int
main ( int argc, char *argv[] )
{
int nummer, uitkomst, teller;
char uitkomst2[10];
nummer = 100 ;
uitkomst = 100 ;
teller = 0 ;
while (uitkomst > 0)
{
uitkomst2[teller++]= uitkomst%2;
uitkomst = uitkomst /2 ;
}
printf ("Het getal %d is binair ", nummer);
while (teller >  0)
{
printf("%d", uitkomst2[teller-1]);
teller = teller - 1;
}
return EXIT_SUCCESS;
}
/*c----------  end of funcrion main  ---------- */```
Roelof

Edit 1 :

I elimated number in the loops.
I use it only for displaying the answer.
But thanks for the help

8. CommonTater

Is my programm good or are there more things I have to learn,
If not, I will try to change this to converting to a octal number.

Roelof

9. Originally Posted by roelof
CommonTater

Is my programm good or are there more things I have to learn,
If not, I will try to change this to converting to a octal number.

Roelof
Well... my thought would be there are always things we ALL have to learn. Programming is a never ending learning opportunity, a gift to those who love a challenge and the nemisis of those who don't.

Your code is working, which for a chapter 1 practice exercise is a pretty good achievement, especially in such a short time. You can be proud of it.

Improvement wise, not much left to work on... 100%....

So, on to the next chapter... don't stray too far from the ordered learning of your texts, they are organized in such a way as to lead you through the process in a well intentioned manner.

If you're thinking of changing it to print octal... Look at the formatters for printf()... I think you'll find you can reduce your code to about 5 lines. Seriously... they beat you to it...

Not too long ago there was a guy here who was trying to do decimal to hexidecimal conversion. This went on for several pages and lots of the guys were chipping in... Finally seeing that it was going noplace, and from my penchant for simplicity, I posted something very close to this...

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

int main (void)
{ unsigned int number;

scanf("%d", &number);
printf("\n0x%8X\n",number);

return 0; }```
The final conclusion was: "I'll bet that teacher never gives that assignment again." LOL...

Keep working my friend, you're doing just fine!

10. Hello,

I know that printf can do the job.
But because in chapter 1 this is not in the text I'm not allowed to use it.
But when looking at the prevoius exercises I have a idea how to do it.
And I think I have to adapt this source a little bit.

Roelof

11. Hello,

I adapted the script so it looks like this:
Code:
``` *
*          Filename:  test2.c
*
*          Description:  conventer from dec to 0101
*
*          Version:  1.0
*          Created:  14-05-11 10:03:25
*          Revision:  none
*          Compiler:  gcc
*
*          Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
*          Company:  FH Südwestfalen, Iserlohn
*
* =====================================================================================
*/

#include        <stdio.h>
#include        <stdlib.h>

int
main ( int argc, char *argv[] )
{
int nummer, uitkomst, uitkomst3, uitkomst4, teller;
char uitkomst2[10] ;
nummer = 21 ;
uitkomst = 21 ;
teller = 0 ;
while (uitkomst > 0)
{
uitkomst3 = (uitkomst % 16) ;
if (uitkomst3 >=10)
{
uitkomst4 = uitkomst3 + 55 ;
}
else
{
uitkomst4 = uitkomst3 + 48 ;
}

uitkomst2[teller++]= uitkomst4;
uitkomst = uitkomst /16 ;
}
printf ("Het getal %d is octal ", nummer);
while (teller >  0)
{
printf("%c", uitkomst2[teller-1]);
teller = teller - 1;
}
return EXIT_SUCCESS;
}```
and Commentator thanks that you would be a friend.

Roelof

12. You might get a better mark, and you might solve minor bugs more easily, if you learn to indent code properly.

Page 3 of 3 First 123