Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Int to binary converstin issues

Status
Not open for further replies.

qkslvrwolf

Programmer
Jul 9, 2002
28
US
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!
 
OK.. I had to do this as well, so I have code for it already.

void intToBits(int number, int numOfBits, char *binary) {
// numOfBits is how long you want the string to be
// ie intToBits(3, 8, binary) would make binary "00000011"

int test;

test = 1 << (numOfBits - 1);

for (int j=0; j < numOfBits; j++) {
if (number & testAgainst) {
binary[j] = '1';
} else {
binary[j] = '0';
}
number <<= 1;
}
}
 
Ooops.. Once again I forgot somehting. Make sure to malloc numOfBits worth of chars into binary before you think about accessing it...

-Skatanic
 
Which is all well and good, but why isn't My code working? I mean, I do appreciate the code, but what I'd really like to know is what I'm missing from my code. Thanks alot!
sean
 
I guess what I really want to know is, even if binary is changed so that the array is only of size 10, why does strlen still return 14 for the size of binary?
 
try memset(binary,0,100) on the binary string array before you do anything. If it is not null terminated, it could be your problem.

matt
 
Thanks to everyone for suggested solutions...I sort of combined some of them/sort of just worked something out. Here is the code that works...//converts an integer to a binary. Destination string *must be empty*.
void int2binary(char *binary, int convertee, int count)
{
int foo;
char ret;
int i = 0;
char tempbinary[100];

//tempbinary = malloc(10);
printf(&quot;%i\n&quot;, convertee);
if (convertee != 0)
{

foo = convertee%2;
int2binary(binary, convertee/2, count+1);
ret = '0' + foo;
binary[count] = ret;
}
else if (convertee == 0)
{
binary[count] = '\0';
}
}

For whatever reason, using strcat to do it was putting in a
eextra 'bel' ascii character, which is apparently a non-printing character between each of the characters I was assigning. Either that, or the fact that I was passing in a char array instead of a char * was. Never did figure out which was the case. Anyway, thanks again!
sean
 
I think I know what was the problem in the first program, you do this:
ret = '0' + foo;
strcat(binary, &ret);

note that in order to make this work you should have done:
ret = '0' + foo;
strncat(binary, &ret, 1);

Since strcat concatenates a string onto another string. You simply took a character ('0') and took it's adress...that means strcat will walk on after the ret-character in memory until it finds a 0-character.
 
Thanks! HTats really pretty cool.

I&quot;m (obviously, I think) new to C and still working out the way it handles pointers, arrays, (and therefore strings).

Thanks again, I'll probably be able to use that later.

sean
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top