hi, i need to convert char to int.
say i have char c ='3' ;
i want to get integer 3 from it ......how can i get it??? how can i convert??
i canot use parseInt() here bocoz "c" is not a string.....what is the way to get an int from a char like above example????
char c = '3';
int i = c - '0';
/* done */
You might want to do sanity checking on c, though, to make sure that it's >='0' and <= '9'. If it's over a certain character, it'll end up as a negative integer which will blow stuff up.
thanks kp.....its ok now.
To iago
---------
".... You might want to do sanity checking on c, though, to make sure that it's >='0' and <= '9'. If it's over a certain character, it'll end up as a negative integer which will blow stuff up.... "
i could not get you.... could you plz give an example what you wanted to mean???
I'm not sure of the java syntax, but probably something like:
int CharToInt(char c) throws SomeException
{
if (c < '0' || c > '9')
throw new ArgumentOutOfRangeException();
return c - '0';
}