Like Tree2Likes
  • 1 Post By Click_here
  • 1 Post By Salem

Decimal to Binary

This is a discussion on Decimal to Binary within the C Programming forums, part of the General Programming Boards category; I'm trying to pass a decimal number to a function and convert it to binary and return it and print ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    100

    Decimal to Binary

    I'm trying to pass a decimal number to a function and convert it to binary and return it and print it out in main. But it prints out 1011 and then seg faults...not sure where it's tripping up

    Code:
    int main(){
      char* binNum = decToBin(25);
      int i = 0;
    
      while(binNum != NULL){
        printf("%c", *(binNum+i));
        i++;
      }
    }
     
     
    char* decToBin(int dec){
        char* output = malloc(20*sizeof(char));
        int i = 0;
        while(dec > 0){
            if(dec % 2){
                output[i] += '1';
            }
            else{
                output[i] += '0';
            }
            dec /= 2;
            i++;
        }
        return output;
    }
    Last edited by johngoodman; 02-26-2013 at 06:52 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    One thread at a time, johngoodman...
    Salem likes this.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Neither get responded to, I'm tempted to make another

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    binNum will never become NULL if malloc succeeded, so the while loop in your main function will run forever (or until SegFault, whichever comes first). You're also printing the binary backwards. There's a few other problems, compile with warning flags.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    I don't see anything having to do with decimal in that code.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    It might be your last if you do.
    How To Ask Questions The Smart Way
    I'm done with cleaning up your "me me me me me" reposting just to keep your thread at the top of the board.

    Keep it here -> Binary to decimal and reverse
    This is closed
    AndiPersti likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    "main" needs to return 0


    binNum needs to be freed to avoid a memory leak


    binNum needs to be tested to see if decToBin failed
    decToBin needs to check to see if malloc failed


    output needs to be (8 * sizeof(dec)) +1 -> 8 bits per byte


    output[i] = '1' or '0', it is not initialised, so += won't work


    The string output needs to have a \0 put on the end of the string.


    This is what you are probably after for that while loop in main
    Code:
    while(*(binNum+i) != '\0')

    And after all that, your string is back the front (25 -> 0b10011) because the left side (msb) was calculated from the original number mod 2, where as the lsb of that number is the original number mod 2


    Enjoy
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal > Binary
    By Gemini2008 in forum C Programming
    Replies: 2
    Last Post: 11-17-2008, 04:28 PM
  2. binary to decimal ! how?
    By o0o in forum C++ Programming
    Replies: 8
    Last Post: 12-23-2003, 10:31 PM
  3. binary - decimal
    By curlious in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2003, 03:25 PM
  4. decimal to binary
    By kurz7 in forum C Programming
    Replies: 8
    Last Post: 07-10-2003, 01:03 AM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM

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