- Mar 31, 2003
- 11,679
- 0
- 81
I actually fixed it using 4 statements. Took out about 200 lines of incredibly inefficient if else statements.
void toBinary( int toConvert, FILE *theFile )
{
if( toConvert == 0 )
{
fprintf( theFile, "%d", toConvert );
}
else if( toConvert == 1 )
{
fprintf( theFile, "%d", toConvert );
}
else
{
toBinary( toConvert/2, theFile );
fprintf( theFile, "%d", toConvert%2 );
}
}
That is my recursive function for converting a decimal into binary.
My problem now is that my negative conversion is wrong. I test if the number is positive or negative and use a fillWith variable to tell the function to fill with leading 1's or 0's. I then convert to positive and send it through my function above. Is there anyway for my function above to be modified to accommodate a 2's compliment number?
Thanks guys,
-Kevin
void toBinary( int toConvert, FILE *theFile )
{
if( toConvert == 0 )
{
fprintf( theFile, "%d", toConvert );
}
else if( toConvert == 1 )
{
fprintf( theFile, "%d", toConvert );
}
else
{
toBinary( toConvert/2, theFile );
fprintf( theFile, "%d", toConvert%2 );
}
}
That is my recursive function for converting a decimal into binary.
My problem now is that my negative conversion is wrong. I test if the number is positive or negative and use a fillWith variable to tell the function to fill with leading 1's or 0's. I then convert to positive and send it through my function above. Is there anyway for my function above to be modified to accommodate a 2's compliment number?
Thanks guys,
-Kevin