# decimal to binary conversion

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 05-14-2011
roelof
decimal to binary conversion
Hello,

The next exercide I have is to convert a decimal to a binary.
Code:

```      int main ( int argc, char *argv[] ) {         int nummer, uitkomst, remainder ;         char uitkomst2 ;         nummer = 4 ;         uitkomst = 4 ;         while (uitkomst <0)         {                 uitkomst = nummer/2;                 remainder = nummer%2;                 if (remainder == 1)                         {                                 uitkomst2 = "1" + uitkomst2 ;                         }                 else                         {                                 uitkomst2 = "0" + uitkomst2;                         }                 nummer = uitkomst;         }         printf ("uitkomst is %s", uitkomst2);         return EXIT_SUCCESS; }                              /* ----------  end of function main  ---------- */```
It won't compile because uitkomst2 = "0/1" + uitkomst2 is wrong.
I get this error :
test2.c|34 col 15| warning: assignment makes integer from pointer without a cast

What I'm trying to achieve is this:

Let says the number is 4
Then 4/2 = 2 with no remainder so it will be a 0
Then 2/2 = 1 with no remainder so it will be a 0
Then 1/2 = 0.5 a remainder so it will be a 1
So uitkomst will be 001

Oke, almost right.

But what Am i thinking wrong here.? I know I have to read the outcome from end to beginning.

Roelof
• 05-14-2011
CommonTater
Well... you actually have the correct answer... but you have it backwards... which is an artifact of displaying the number as you decode it...

Think about it... you are breaking the number down right to left but printing left to right...

Try to think of a way to burst the number from left to right... (since you can't reverse the printing) ... It's not hard to do, but it does require a different approach.
• 05-14-2011
WaltP
Why are you trying to add the string value "1" to a single character?
• 05-14-2011
CommonTater
Quote:

Originally Posted by WaltP
Why are you trying to add the string value "1" to a single character?

Because he's still learning... Your observation is correct and it should be fixed... but first he has to work out the process.
• 05-14-2011
roelof
Hello,

I did that because i work backwards,
So I thought if I take the old piece and put a 1 or 0 before it I don't have to revert the printing.

Only thing that comes in mind is to divide till the it's 0.
Then you have the first number and work backwards.
But then you talk about recursion and that's not discussed in the book yet.

So still thinking and use google.

Roelof

Edit 1 :

I have found this page : How to Convert from Decimal to Binary - wikiHow
And I can use this method : Comparison with descending powers of two and subtraction
But the downside of this method is that if a user entertered a big number I need a lot of substractions.
So how big must the last number be.
• 05-14-2011
roelof
Hello

Anyone who can shine a light on this.

With the method1 (which Im using now) I have no idea how to store the 0 and 1 on the right place because you can't know how many 0 en 1 you need. You can store in a a array and read it from the last to the first but you don't know which number the lastest entry has.

Method2 jhas the disadvantage that you first has to know which power of 2 is the nearest.

Roelof
• 05-14-2011
tabstop
Quote:

Originally Posted by roelof
With the method1 (which Im using now) I have no idea how to store the 0 and 1 on the right place because you can't know how many 0 en 1 you need. You can store in a a array and read it from the last to the first but you don't know which number the lastest entry has.
Roelof

Except, of course, you do. How else do you know where to put the element in the array when you compute it, if you don't know where the end of the array is?
• 05-14-2011
roelof
Oke

of course I can use let's say a array of 20.
And every time I divide it with 2 a sort of counter can be used to put a array in it
So what i mean it array[place++] = remainder.

It that what you mean or do we misunderstood each other ?

Roelof
• 05-14-2011
tabstop
Quote:

Originally Posted by roelof
Oke

of course I can use let's say a array of 20.
And every time I divide it with 2 a sort of counter can be used to put a array in it
So what i mean it array[place++] = remainder.

It that what you mean or do we misunderstood each other ?

Roelof

That's what we mean. And when you get to the end you can start at place (well, really, place-1 the way you have it written) to go backwards.
• 05-14-2011
roelof
Oke,

I see one problem with this method.
We cannot know how big the array must be,
If the user enters a really big number I need a big array for all the 1 and 0.

Roelof
• 05-14-2011
tabstop
It doesn't matter what the user types in; even if the user types in 99999999999999999999999999999999999999999999999999 99999999999999999 the computer will not store a number larger than the data type can handle.
• 05-14-2011
roelof
Oke,

But that means if the user enters that number the outcome will not be bigger then 20 so the outcome is not right.

I think if the divine reached the 21 number and try to store it it gives a error because you are outside the boundarys of that array.

Or im mistaken here ?

Roelof
• 05-14-2011
CommonTater
Quote:

Originally Posted by roelof
Oke,

I see one problem with this method.
We cannot know how big the array must be,
If the user enters a really big number I need a big array for all the 1 and 0.

Roelof

Sure we do... 32 for 32 bit binary, 64 for 64 bit binary, etc... Then you limit the user's input to fit the array.

But there is a simpler way still.... (although your course may not have covered bitwise functions, yet)

For a simple 32 bit signed integer containing a positive value...

Code:

```int number = 112323; // for example int filter = 0x40000000u;  // msb = 1; while (filter)   { if (number & filter)       printf("1");     else       printf("0");     filter = filter >> 1; } printf("\n\n");```
• 05-14-2011
roelof
Oke,

I understand that,
But how can I know if a number is a 32 bit binary or a 64 bit binary.
In my opinion we can know that after the conversion.

Roelof
• 05-14-2011
tabstop
Quote:

Originally Posted by roelof
Oke,

I understand that,
But how can I know if a number is a 32 bit binary or a 64 bit binary.
In my opinion we can know that after the conversion.

Roelof

That depends on the hardware on your machine, and how your environment talks to the hardware. (In other words: when you type "int" you will get either a 32-bit or a 64-bit integer (or I suppose a 16-bit integer in the worst case), depending on your particular system. You will always get the same-sized integer every time, until you do something like buy a new system or a new compiler.)
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last