Author |
Message |
gitoxa
|
Posted: Tue Dec 09, 2008 6:16 pm Post subject: Decimal to Binary |
|
|
I'm fairly new to c++ and I've been having trouble trying to code a recursive decimal to binary converter. I'm trying to pass an integer into the function and return a string in the end. I'm not sure if I'm going about this the right way though. Tips preferred over solutions if possible, as I'm trying to learn.
Here's my code which compiles fine in Dev-CC++ 4.9.9.2, but I get some... weird output.
c: | #include <iostream>
#include <string>
using std:: string;
//A function that convers a decimal number to a binary number, stored in a string variable
string Dec_To_Bin (int Num )
{
if (Num > 0)
{
return Dec_To_Bin (Num / 2) + (char) (Num % 2);
}
else
{
return "";
}
}
int main ()
{
int DecNum;
string BinNum;
std:: cout << "Decimal to binary converter, enter a positive whole decimal number\nDecimal: ";
std:: cin >> DecNum;
BinNum = Dec_To_Bin (DecNum );
std:: cout << "Binary: " << BinNum << std:: endl;
system ("PAUSE");
return 0;
} |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Saad
|
Posted: Tue Dec 09, 2008 6:48 pm Post subject: Re: Decimal to Binary |
|
|
c++: |
return Dec_To_Bin(Num / 2) + (char) (Num % 2);
|
More specifically, Does not do what you think it might. While (Num % 2) returns 0 or 1, casting that does does not return the character '1' or '0'. Instead when you cast it to a character, your converting the value to the character with the ASCII value of 0 or 1. You can either add a value to the result and cast it to a character. Hint think of what the ASCII value of '0' and what 0 or 1 plus that value will return and cast the value that reflects the ASCII value of '0' or '1'.
Alternatively, you can look up the functions to convert an integer to a string. |
|
|
|
|
|
gianni
|
Posted: Tue Dec 09, 2008 6:51 pm Post subject: Re: Decimal to Binary |
|
|
Tips
I think the best way to do this may not be through recursion. You can set this up in a simple for or while loop. Another thing to keep in mind (*wink*) is that each bit in a binary string has a maximum value of 2^(bit position). |
|
|
|
|
|
gitoxa
|
Posted: Tue Dec 09, 2008 8:51 pm Post subject: Re: Decimal to Binary |
|
|
gianni @ Tue Dec 09, 2008 6:51 pm wrote: Tips
I think the best way to do this may not be through recursion. You can set this up in a simple for or while loop. Another thing to keep in mind (*wink*) is that each bit in a binary string has a maximum value of 2^(bit position).
I already understand recursion enough for the concept of it not to confuse me, I'm trying to just get the coding side of it done in c++
I fiddled around with the idea of it being the ASCII value, but I'm short on time right now so I'll fiddle around with that more later. I found some code for the casting of it, and it works now. |
|
|
|
|
|
md
|
Posted: Wed Dec 10, 2008 12:04 pm Post subject: RE:Decimal to Binary |
|
|
You seem to be on the right track - though as has been pointed out you need to properly convert to a character.
You should also look at stringstreams and how to convert numbers to strings using them. It should be possible to convert to binary though I do not know the exact code. |
|
|
|
|
|
gitoxa
|
Posted: Wed Dec 10, 2008 2:06 pm Post subject: Re: RE:Decimal to Binary |
|
|
md @ Wed Dec 10, 2008 12:04 pm wrote: You should also look at stringstreams and how to convert numbers to strings using them.
This is what I used to convert them. There was also the function itoa() but it was being a pain to implement with my current knowledge |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Sun Dec 14, 2008 5:45 pm Post subject: Re: Decimal to Binary |
|
|
Why not recursion? No function call overhead (and the stack space required).
What was the initial converting hint? Add a value to the 0 or 1 to get '0' or '1'.
Why not use bitwise-and to test the low bit? Why not use logical right shift to drop that bit? |
|
|
|
|
|
|