Thread: Decimal to binary conversion with character string

  1. #1
    Registered User
    Join Date
    Feb 2011
    Location
    San Diego
    Posts
    10

    Decimal to binary conversion with character string

    So I'm taking a new class that delves into assembly, but our professor is testing our C knowledge beforehand. This what he asks of us:

    Return the binary representation of a number x as a
    character string.
    For example : if x = 17 is passed to the function the
    return value should be a pointer to the string
    00000000000000000000000000010001.

    Now, main and the function name are exactly what he has provided. So I need to basically write the function and return the string. What I was trying to do was call the function and use the value suggested (17), do the modulus to find the remainders, store that info, and then finally swap the stored values in the string so it will print in the right order (msb to lsb). My C programming class lightly covered strings and pointers so I am not great in that area, but I'm giving it my best shot. Any help or suggestions is appreciated. Thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    char *int_to_binary(int x);
    
    int main(void) {
    	int x = 17;
    
    	printf("%s\n", int_to_binary(x));
    
    	return 0;
    }
    
    char *int_to_binary(int x)	{
    	int i, j, temp;
    	char bin[32];
    
    	for(i=0; i<=31; i++) {
    		bin[i]=x%2;
    		x=x/2;
    	}
    	printf("%s\n", bin);
    	for(j=0;j<=16; j++)	{
    		temp = bin[0+j];
    		bin[0+j] = bin[31-j];
    		bin[31-j] = temp;
    	}
    
    	return bin;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Returning a pointer to a local variable is undefined. You cannot directly return bin to the caller: if the caller later tried to use the returned pointer, it could segfault. You could instead accept a pointer to a buffer from the caller, copy bin over, and return the pointer you got from the caller. That presents no issues.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. You are returning the address of a local variable when you pass bin back to main. This is a no no.
    2. To print using %s, what is being printed must be a valid null terminated string. Your buffer does not contain space for a null terminating character.
    3. You are assigning integer values of 0, 1 to bin. You must instead assign character values '0', and '1'. These two things are not the same.
    4. You can get rid of the second "reverse" loop if you make your first loop go from 31 down to 0 instead of going from 0 up to 31.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You logic is essentially correct. There are a few issues with your string:
    1) Storing binary 0 and 1 into each element is not equivalent to having a printable ASCII string. You must convert to ASCII:
    bin[i]=x%2 + '0';
    2) since you are going to treat this as a string you must terminate it with a null:
    bin[32] = '\0'; - before printing it.
    3) Because of the above extra character the string should be defined as:
    char bin[33];
    4) The 'for' loop is not correct. Should be:
    for(j=0;j<16; j++)
    5) As whiteflags mentioned, the string is better off being in global. If you define an array (string) inside a function it resides on the stack and is not accessible when the function terminates. Even if you still have a pointer to it.

    (Sorry, many points overlap with above post. I was busy composing this and did not see the above.)
    Last edited by nonoob; 02-06-2012 at 12:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary to decimal conversion
    By caysonmars in forum C Programming
    Replies: 9
    Last Post: 06-17-2011, 10:24 PM
  2. decimal to binary conversion
    By roelof in forum C Programming
    Replies: 41
    Last Post: 05-16-2011, 03:32 AM
  3. Decimal to Binary conversion help
    By tru.cutru in forum C Programming
    Replies: 3
    Last Post: 07-08-2008, 10:17 PM
  4. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 09:51 PM
  5. Binary to decimal conversion help!
    By matrism in forum C Programming
    Replies: 4
    Last Post: 03-25-2002, 12:22 PM