81

(I'm new at Java programming)

I have for example:

char x = '9';

and I need to get the number in the apostrophes, the digit 9 itself. I tried to do the following,

char x = 9;
int y = (int)(x);

but it didn't work.

So what should I do to get the digit in the apostrophes?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Haim Lvov
  • 903
  • 2
  • 10
  • 20

4 Answers4

174

The ASCII table is arranged so that the value of the character '9' is nine greater than the value of '0'; the value of the character '8' is eight greater than the value of '0'; and so on.

So you can get the int value of a decimal digit char by subtracting '0'.

char x = '9';
int y = x - '0'; // gives the int value 9
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 3
    @HaimLvov: To elaborate... Take a look at the ASCII table anywhere online. Any `char` has the numeric value of its equivalent decimal value in that table. So you can subtract any one from any other to get a numeric result. It just so happens, naturally, that the characters 0 through 9 are in order so the math works. – David Sep 21 '17 at 12:19
  • more about https://stackoverflow.com/questions/3195028/please-explain-what-this-code-is-doing-somechar-48 – Andrew Tobilko Sep 21 '17 at 12:20
  • 2
    @David - Is this answer better than Character.getNumericValue(x) in terms of performance ? – MasterJoe Jul 06 '20 at 18:55
  • 1
    In ASCII '0' = 48, 1='49' etc. Found a helpful explanation about the theory here: https://beginnersbook.com/2019/04/java-char-to-int-conversion/ – nanospeck Jul 11 '20 at 00:44
  • A nice ASCII table can be found here : https://www.alpharithms.com/ascii-table-512119/ – magicsign Mar 28 '22 at 14:46
58

I you have the char '9', it will store its ASCII code, so to get the int value, you have 2 ways

char x = '9';
int y = Character.getNumericValue(x);   //use a existing function
System.out.println(y + " " + (y + 1));  // 9  10

or

char x = '9';
int y = x - '0';                        // substract '0' code to get the difference
System.out.println(y + " " + (y + 1));  // 9  10

And it fact, this works also :

char x = 9;
System.out.println(">" + x + "<");     //>  < prints a horizontal tab
int y = (int) x;
System.out.println(y + " " + (y + 1)); //9 10

You store the 9 code, which corresponds to a horizontal tab (you can see when print as String, bu you can also use it as int as you see above

azro
  • 53,056
  • 7
  • 34
  • 70
26

You can use static methods from Character class to get Numeric value from char.

char x = '9';

if (Character.isDigit(x)) { // Determines if the specified character is a digit.
    int y = Character.getNumericValue(x); //Returns the int value that the 
                                          //specified Unicode character represents.
    System.out.println(y);
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2787184
  • 3,749
  • 10
  • 47
  • 81
2

If you want to get the ASCII value of a character, or just convert it into an int, you need to cast from a char to an int.

What's casting? Casting is when we explicitly convert from one primitve data type, or a class, to another. Here's a brief example.

public class char_to_int
{
  public static void main(String args[])
  {
       char myChar = 'a';
       int  i = (int) myChar; // cast from a char to an int
       System.out.println ("ASCII value - " + i);
  }

In this example, we have a character ('a'), and we cast it to an integer. Printing this integer out will give us the ASCII value of 'a'.

Amol Raje
  • 928
  • 3
  • 9
  • 16
  • OP doesn't want to get the ASCII value, he wants to get the value when interpreting this character as an actual number. For '9' he wants to return 9 rather than 57 (the ASCII value of '9') – Adam Burley May 28 '21 at 14:07