qkslvrwolf
Programmer
I'm trying to convert an int into a binary string of a given length. here is my function...
// converts an integer to a binary. Destination string *must be empty*.
void int2binary(char binary [100], int convertee)
{
int foo;
char ret;
int i = 0;
printf("%i\n", convertee);
if (convertee != 0)
{
foo = convertee%2;
int2binary(binary, convertee/2);
ret = '0' + foo;
strcat(binary, &ret);
}
else if (convertee == 0)
{
while ((binary == '1') || (binary == '0'))
{
i++;
}
binary = '\0';
}
}
However, this returns, for say 127, a binary string that looks correct when printed (1111111), but when I run strlen on it later in order to turn it into 001111111, it says that it is taking up 14 characters. I understand that if a string is 7 characters, strlen returns 8, but nevertheless, I can't figure out why its returning 14..
thanks!
// converts an integer to a binary. Destination string *must be empty*.
void int2binary(char binary [100], int convertee)
{
int foo;
char ret;
int i = 0;
printf("%i\n", convertee);
if (convertee != 0)
{
foo = convertee%2;
int2binary(binary, convertee/2);
ret = '0' + foo;
strcat(binary, &ret);
}
else if (convertee == 0)
{
while ((binary == '1') || (binary == '0'))
{
i++;
}
binary = '\0';
}
}
However, this returns, for say 127, a binary string that looks correct when printed (1111111), but when I run strlen on it later in order to turn it into 001111111, it says that it is taking up 14 characters. I understand that if a string is 7 characters, strlen returns 8, but nevertheless, I can't figure out why its returning 14..
thanks!