How can I convert a char to int in Java?

233,267

Solution 1

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

Solution 2

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

Solution 3

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);
}

Solution 4

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'.

Share:
233,267
Haim Lvov
Author by

Haim Lvov

Graduated high school student. I've learned (and still I'm) Java, C, C++, Python, Python networking, Backend development with Spring Boot and Android development.

Updated on July 05, 2022

Comments

  • Haim Lvov
    Haim Lvov almost 2 years

    (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?

  • David
    David over 6 years
    @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.
  • Andrew Tobilko
    Andrew Tobilko over 6 years
  • MasterJoe
    MasterJoe almost 4 years
    @David - Is this answer better than Character.getNumericValue(x) in terms of performance ?
  • nanospeck
    nanospeck almost 4 years
    In ASCII '0' = 48, 1='49' etc. Found a helpful explanation about the theory here: beginnersbook.com/2019/04/java-char-to-int-conversion
  • Adam Burley
    Adam Burley almost 3 years
    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')
  • magicsign
    magicsign about 2 years
    A nice ASCII table can be found here : alpharithms.com/ascii-table-512119