1

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Onyr
  • 769
  • 5
  • 21
  • [Java: parse int value from a char](https://stackoverflow.com/questions/4968323/java-parse-int-value-from-a-char) – Ole V.V. Dec 16 '18 at 19:25

5 Answers5

2

A char is just a smaller int with fancy printing. When you convert it to an int you lose this fancy printing and just print the Unicode value of the character. The unicode value of the character 0 is 48.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

s.charAt(2) will give you '0', the character 0, NOT the number 0. The ascii char code of '0' is 48.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
luk2302
  • 55,258
  • 23
  • 97
  • 137
0

When you parse a char to an int, you get the index of the char in the ASCII-Table.

Digits start with 0 at index 48 which is why (int)'0' returns 48. As the indexes for numbers increment proportional to the numbers, you could use

int charAt2 = (int)s.charAt(2) - 48;
L. Männer
  • 446
  • 2
  • 15
0
String s = "11001";
int charAt2 = Integer.valueOf(""+s.charAt(2));
System.out.println("charAt2 = "+charAt2);

a possible way is to do this. valueOf converts a String to int, ""+aChar is a quick and dirty way to convert a char to String

0

A Complete final answer :

String s = "11001";
int charAt2 = (int)s.charAt(2); //Why this gives 48 ???
System.out.println("charAt2 = "+charAtI);

A quick reminder: In this situation, 11001 is a String representing a number in radix 2 (binary). In the more common radix 10 (decimal) we use everyday, (binary) 11001 => (decimal) 1*2^4 + 1*2^3 + 0*2^2 + 0*2^1 + 1*2^0 = 25.

Possible confusions: Do not confuse. Here we don't want to convert the String "11001" into a character by returning his ASCII code, we want to convert the char 0 located at the index 2 of the string "11001" into a number in radix 10, namely 0.

NB: Realize that the function String.charAt(x) returns the character located at the String's specified index, with indexes starting from zero.

Answer: The code above return 48 instead of 0 because the (int) conversions happen after the s.chatAt(2) which returns the char "0" from the String s="11001". This char 0 is then converted into an int. Due to the ASCII representation of char, the char "0" is represented by the int 48 and is converted accordingly.

Onyr
  • 769
  • 5
  • 21