• (2 Pages)
• 1
• 2

27 Replies - 1840 Views - Last Post: 08 June 2013 - 10:53 PM

#16 jon.kiparsky

• Pancakes!

Reputation: 8758
• Posts: 15,073
• Joined: 19-March 11

Posted 08 June 2013 - 12:40 AM

```while mynum is not zero
multiply binary by ten, add on the least significant digit of the number to be converter, and reduce the number to be converted by half.
```

Notice that this is going to give you a deceptive number - the machine will think of this as being a decimal number, so that's not great. You might want to think about assembling this as a String, not as a number.

But that's not your real problem here, of course. Remember that you're peeling number off the least-significant side, and reducing from the least-significant side. That means that after each round, the numbers you're dealing with are more significant - not less.

#17 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

Posted 08 June 2013 - 12:45 AM

Yes, I know, that's why it outputs reversed, I edited afterwards ... is that ok now ? And I used integers so I will be able to do this easily in C++ so I would see the result quickly without gettings strings involved.

#18 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

Posted 08 June 2013 - 12:50 AM

```#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
int decimal;
char ch[2];
cin >> decimal;
string binary;

while (decimal != 0)
{
itoa (decimal%2, ch, 10);
binary.insert(binary.begin(), ch[0]);
decimal = decimal / 2;
}

cout << binary <<endl ;

system("pause");

return 0;
}
```

This outputs it right, but what I was asking is if the algorithm is the one you were talking about /> or I didn't get it right ?

EDIT: changed a to decimal

This post has been edited by TwoOfDiamonds: 08 June 2013 - 01:23 AM

#19 jon.kiparsky

• Pancakes!

Reputation: 8758
• Posts: 15,073
• Joined: 19-March 11

Posted 08 June 2013 - 06:34 AM

Looks good to mw!

#20 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

Posted 08 June 2013 - 06:38 AM

Thank you a lot sir !

#21 jon.kiparsky

• Pancakes!

Reputation: 8758
• Posts: 15,073
• Joined: 19-March 11

Posted 08 June 2013 - 07:05 AM

No problem - glad I could help!

#22 baavgai

• Dreaming Coder

Reputation: 6298
• Posts: 13,462
• Joined: 16-October 07

Posted 08 June 2013 - 07:27 AM

Looks good, but you're making that harder on yourself than it needs to be. You don't need itoa because you know exactly what value you should have in either case.

Perhaps:
```while (decimal != 0) {
char ch = '0';
if (decimal%2==1) { ch='1'; }
binary.insert(binary.begin(), ch);
decimal /= 2;
}

```

Or, more tersely:
```while (decimal != 0) {
binary.insert(binary.begin(), ((decimal%2)?'1':'0'));
decimal /= 2;
}

```

#23 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

Posted 08 June 2013 - 07:36 AM

I thought about the first solution too :-? but sincerely, the second one seems much cleaner and by cleaner I mean no extra functions or variables.

#24 jon.kiparsky

• Pancakes!

Reputation: 8758
• Posts: 15,073
• Joined: 19-March 11

Posted 08 June 2013 - 11:05 AM

baavgai, on 08 June 2013 - 09:27 AM, said:

Looks good, but you're making that harder on yourself than it needs to be. You don't need itoa because you know exactly what value you should have in either case.

True, but this cuts out the possibility of generalizing to arbitrary bases. (which seems to me the whole point of the exercise)

It's very easy to see how you change TwoOfDiamonds' version to make a function which converts from a string in base b into decimal - and from there, a generic base conversion routine. Can't really do that when you tie yourself to binary this way.

#25 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

Posted 08 June 2013 - 01:20 PM

Jon, could you pretty please explain to me what you were saying in the previous post ? I didn't really understood.

#26 baavgai

• Dreaming Coder

Reputation: 6298
• Posts: 13,462
• Joined: 16-October 07

Posted 08 June 2013 - 03:39 PM

jon.kiparsky, on 08 June 2013 - 02:05 PM, said:

this cuts out the possibility of generalizing to arbitrary bases.

Fair enough, if that's the goal. However, itoa would still not be your friend here. You'd want a list of base values, not something tied to a base 10 assumption.

e.g.
```string toBase(int n, int baseSize, char *symbols);

```

#27 jon.kiparsky

• Pancakes!

Reputation: 8758
• Posts: 15,073
• Joined: 19-March 11

Posted 08 June 2013 - 04:23 PM

TwoOfDiamonds, on 08 June 2013 - 03:20 PM, said:

Jon, could you pretty please explain to me what you were saying in the previous post ? I didn't really understood. />

Just saying that all conversions from the internal representation (which, at the level you access it, is pretty much a pure quantity, if you think about it) to some base notation (base 2, base 3, base 64, you name it) are performed the same way: you peel off the least significant digit, which depends on your chosen base and not on the pure quantity you're dealing with, and you paste it into your string representation. Using binary logic to smooth out the binary case makes it impossible to generalize this out to bases other than 2, so it's a step in the wrong direction, unless you're interested in premature optimizations for their own sake. (as we know from Knuth, all optimizations are premature unless proved otherwise)

It's relatively easy to represent bases up to base 36, using the digits plus the 26 alphabetic characters. Go ahead and write a function that takes a value and a base, and displays the value in the chosen base. Then, if you like, write something that takes a string representing a number in base b and converts it to an int. And for giggles, wire the two together to convert hex to base 19 or something.

#28 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12