Thread: decimal to binary conversion

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    decimal to binary conversion

    Hello,

    The next exercide I have is to convert a decimal to a binary.
    So I made this :
    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
    Last edited by roelof; 05-14-2011 at 02:31 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Why are you trying to add the string value "1" to a single character?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by WaltP View Post
    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.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    230
    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.
    Last edited by roelof; 05-14-2011 at 03:44 AM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by roelof View Post
    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?

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by roelof View Post
    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.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    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");
    Last edited by CommonTater; 05-14-2011 at 10:15 AM.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by roelof View Post
    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.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to Binary conversion help
    By tru.cutru in forum C Programming
    Replies: 3
    Last Post: 07-08-2008, 10:17 PM
  2. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 09:51 PM
  3. Decimal to binary conversion
    By blckspder in forum C Programming
    Replies: 5
    Last Post: 04-07-2005, 12:38 PM
  4. decimal to binary conversion
    By noob2c in forum C Programming
    Replies: 4
    Last Post: 05-29-2003, 08:07 PM
  5. Binary to decimal conversion help!
    By matrism in forum C Programming
    Replies: 4
    Last Post: 03-25-2002, 12:22 PM