CodeGym /Java Blog /Java Types /Java Convert Char to Int With Examples
Author
Oleksandr Miadelets
Head of Developers Team at CodeGym

Java Convert Char to Int With Examples

Published in the Java Types group
Pretty often symbolic information that users enter from the keyboard needs to be converted to numbers or a programmer should get numbers from it. There are some methods in Java to do it. In this article, we will figure out the methods to help us convert char to int value.

Implicit Type сasting

Type casting is the process of converting a value of one data type into a value of another data type. Type casting could be manual or automatically depending on types. The types should be compatible. In Java we’ve got two types of typecasting, explicit and implicit. Implicit can only be done if we convert the variable from "larger" to "smaller" type (say, from int to long). If we want to convert a variable of type char to int data type, most often we need to get its equivalent value from the ASCII table. Every alphabet or symbol that can be entered digitally has a corresponding unique integer number. For example, the alphabet ‘A’ has an ASCII code of 65. By type casting, we’re forcefully converting the character value into its equivalent ASCII integer code. Here is our Java char to int example using type casting.

Java char to int example (using typeCasting)


package charToInt;

public class Implicit {

       public static void main(String[] args) {
           char char1 = 'A';
           char char2 = 'a';
           int x = char1;
           int y = char2;

           System.out.println("ASCII value of '" + char1 + "' is " + x);
           System.out.println("ASCII value of '" + char2 + "' is " + y);
       }
   }
The output is:
ASCII value of 'A' is 65 ASCII value of 'a' is 97

Explicit type casting

As we saw above, in the case of char to int conversion, it is not necessary to do this explicitly, since 2 bytes are used to store char, and 4 bytes are used to store int. That is, the int type is "larger". Nevertheless, as an exercise, we can explicitly cast a char variable to an integer. For this operation, we mention (int) in parentheses.

package charToInt;

public class Explicit {

       public static void main(String[] args) {
           char char1 = 'A';
           System.out.println("ASCII value of '" + char1 + "' is " + (int)char1);
       }
   }
The output is:
ASCII value of 'A' is 65

Using getNumericValue()

getNumericValue() works similarly to type casting, but instead of following the ASCII table, it follows the Unicode encoding standard. ASCII can only represent a specific set of symbols and characters such as latin letters (plus some national alphabet symbols), numbers, punctuation and control characters. Unicode contains over a million values containing letters and symbols for every language in the world. For example, the value of A in Unicode is \u0041 which is equivalent to a numeric value of 10. If the character doesn’t have a numeric value, the method returns -1. Using this method could be convenient to return non-negative integers.

package charToInt;

public class UsingGetNumericValue {

       public static void main(String[] args) {
           char char1 = '7';
           char char2 = 'A';
           char char3 = '*';

           int x = Character.getNumericValue(char1);
           int y = Character.getNumericValue(char2);
           int z = Character.getNumericValue(char3);

           System.out.println("The Unicode value of '" + char1 + "' is " + x);
           System.out.println("The Unicode value of '" + char2 + "' is " + y);
           System.out.println("The Unicode value of '" + char3 + "' is " + z);

       }
   }
The output is:
The Unicode value of '7' is 7 The Unicode value of 'A' is 10 The Unicode value of '*' is -1

Using ParseInt()

ParseInt() is another method to convert the char to int. It's also widely used to cross convert between other numeric data types as well such as a float, double, and long. ParseInt() requires at least one argument and at the most, two arguments. The first argument is the variable name of the value we’re converting. The second argument is the radix which refers to the base value of decimal, octal, hexadecimal, etc. Due to the ability of ParseInt() to convert data types with the number system in mind as well, it's an optimal choice for converting “numerical” char to int in Java. There are two important points to keep in mind if you want to use this method. parseInt() method accepts String arguments only, so you should first convert char to String (using String.valueOf). You can’t use parseInt() to convert the alphabets or other symbols except numeric values stored in the char data type. If you input non-numeric values, you'll get an error. Here is a Java example:

package charToInt;

public class ParseInt {
       public static void main(String args[]){
           char ch = '7';
           int n = Integer.parseInt(String.valueOf(ch));
           System.out.println(n);
       }
   }
The output is:
7
Let’s try to use parseInt ton non-numeric value:

package charToInt;

public class ParseInt {
       public static void main(String args[]){
           char ch = 'q';
           int n = Integer.parseInt(String.valueOf(ch));
           System.out.println(n);
       }
   }
The output is:
Exception in thread "main" java.lang.NumberFormatException: For input string: "q" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at charToInt.ValueOf.main(ValueOf.java:6)

Subtracting ‘0’

There is a very simple way to convert a character to an integer. To do this, simply subtract the ASCII value of the character "0" from the character. For example, to get int 7 from character "7":

int intValue = '7'-' 0 ';
Note, that this works for getting int values ​​exclusively for integer characters! If you subtract '0' from, say, 'A', the method will simply return the difference between the ASCII codes of zero and the letter A. Here's an example.

package charToInt;

public class SubtractingZero {

   public static void main(String[] args) {
       char char1 = '7';
       char char2 = 'A';
       char char3 = '*';

       System.out.println(char1);
       System.out.println(char1 - '0');

       System.out.println(char2);
       System.out.println(char2 - '0');

       System.out.println(char3);
       System.out.println(char3 - '0');
      
   }

}
The output is:
7 7 A 17 * -6

Conclusions

If you need to convert char to int in Java use one of the methods:
  • Implicit type casting //getting ASCII values
  • Character.getNumericValue()
  • Integer.parseInt() //in pair with String.valueOf())
  • Subtracting ‘0’ //works for integer numeric values only
You can also do explicit type casting. However, this is a redundant operation: it is not needed, but just works in Java.
Comments (2)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Peter Gray Level 20, Compiegne, France
23 October 2020
The last example throws exception: Exception in thread "main" java.lang.NumberFormatException: For input string: "a" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770)