Author | Message | Time |
---|---|---|
touchstone | 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???? | January 25, 2004, 4:52 PM |
Kp | [code]char c = '3'; int i = c - '0'; /* done */[/code] | January 25, 2004, 5:03 PM |
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. | January 25, 2004, 5:18 PM |
touchstone | 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??? | January 25, 2004, 8:58 PM |
K | I'm not sure of the java syntax, but probably something like: [code] int CharToInt(char c) throws SomeException { if (c < '0' || c > '9') throw new ArgumentOutOfRangeException(); return c - '0'; } [/code] | January 25, 2004, 9:09 PM |