# Thread: Decimal to binary conversion with character string

1. ## 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. 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.

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.

3. 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.)