Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

Faisal syed
Ranch Hand
Posts: 35
Hi Friends,

Can you guide me on how to add 2 characters say a= '1' , b = '2' to yield a 3 (Not 99 ie 49 + 50; the ascii values)?

Thanks
Faisal

Faisal syed
Ranch Hand
Posts: 35
There is a constraint that this needs to be done without wrapper classes

Faisal syed
Ranch Hand
Posts: 35
Is it that we need to traverse through all the ascii values, and lookup until we find a match ?

Jeff Verdegan
Bartender
Posts: 6109
6
So you have the character '1', which has a numerical value of 49, but you want to treat it as numerical value 1, and you have character '2', which has a numerical value of 50 but you want to treat it as a numerical value of 2?

So, you want the following mapping?

Yes? Look at the pattern above, and also consider the fact that you can do arithmetic on characters. Does that give you an idea of how to approach this?

Faisal syed
Ranch Hand
Posts: 35
I get the fact that I need to have a map.
I realise we can do arithmetic, but the results would be different.

char 'a' + 'b' = 99// not 3

Thanks
Faisal

Campbell Ritchie
Sheriff
Posts: 48972
60
Jeff said mapping, not a Map. There is a much simpler way to do such arithmetic, using chars only.

Faisal syed
Ranch Hand
Posts: 35
Hi Ritchie,

yes. I also meant mapping.

[There is a much simpler way to do such arithmetic, using chars only. ]
Could you give an example please ?

Stuart A. Burkett
Ranch Hand
Posts: 679
Faisal syed wrote:Hi Ritchie,

yes. I also meant mapping.

[There is a much simpler way to do such arithmetic, using chars only. ]
Could you give an example please ?

If you had the value x but you actually wanted the value 0, what would you take away from x to get 0.
If you had the value (x + 1) but you actually wanted the value 1, what would you take away from (x + 1) to get 1.
If you had the value (x + 2) but you actually wanted the value 2, what would you take away from (x + 2) to get 2.

Campbell Ritchie
Sheriff
Posts: 48972
60
Faisal syed wrote: . . . yes. I also meant mapping. . . .
But you said map, which is different from mapping.
Could you give an example please ?
Stuart B has already given you as much information as you really need.

Henry Wong
author
Marshal
Posts: 21123
78
Campbell Ritchie wrote:
Faisal syed wrote: . . . yes. I also meant mapping. . . .
But you said map, which is different from mapping.
Could you give an example please ?
Stuart B has already given you as much information as you really need.

This may be a case where the OP should step back, take out a pencil and paper, and work it out first. Everyone here seems to easily envision what needs to be done, but I would guess that the reason for this, is that we have done it many many times before. Someone who has never done this should work it out on paper -- even though the maths behind it is ridiculously easy.

Henry

Campbell Ritchie
Sheriff
Posts: 48972
60
I see what you mean, Henry. Sorry.