Here was my problem : I wanted to convert a char into an int from a string using the following code :
String s = "11001";
int charAt2 = (int)s.charAt(2); //Why this gives 48 ???
System.out.println("charAt2 = "+charAtI);
I get on the terminal: 48. Why? That's what I would like to understand.
I have already found a great fix for my problem on Stack Overflow thanks to dasblinkenlight's link type conversions. As I think it can be useful for someone else, here is the fixed version that perfectly works. I use the method Character.digit( char c, int i)
that give an int from the char c
in the radix i
to do the conversion.
String s = "11001";
int charAt2 = Character.digit(s.charAt(2), 2); //radix 2 here
System.out.println("charAt2 = "+charAt2);
On the terminal : 0. This works.