4 ways to convert char to int in Java [Practical Examples]

Different ways to convert char to int in Java

In Java, the char and an int are a primitive data types. In real life applications, many times we will need to convert the value of one type to the other type. So, Java provides the mechanism for this conversion. As we know, the char type in java can hold a single character whereas an int type holds the numbers without decimal. Here, we will see several ways to convert a char to its equivalent int.

  • Using ASCII Values
  • Using Integer.parseInt() method
  • Using Character.getNumericValue() method
  • Subtracting '0'

 

Using ASCII Values

This is the simplest approach to convert char to int in Java. Here, there are two ways to accomplish this task. The first way is through an implicit type conversion, where we assign the value of char variable to int variable. So, it stores the equivalent ASCII value in int type. The other way is explicit type conversion, where we do not use int variable but we write a int in the bracket for type conversion.

Advertisement

Example : In this example, we are showing both implicit and explicit type conversions.

// Program to convert char to int
public class Main {
    public static void main(String[] args) {
        // Initializing the variables
        char a = '1';
        char b = '2';

        // Implicit type conversion
        int a1 = a;
        int b1 = b;
        System.out.println("The ASCII value of " + a + " is " + a1);
        System.out.println("The ASCII value of " + b + " is " + b1);

        // Explicit type conversion
        a = '5';
        b = '6';
        System.out.println("The ASCII value of " + a + " is " + (int) a);
        System.out.println("The ASCII value of " + b + " is " + (int) b);

    }
}

Output

The ASCII value of 1 is 49
The ASCII value of 2 is 50
The ASCII value of 5 is 53
The ASCII value of 6 is 54

 

Using Integer.parseInt() method

In this approach, we are using the static method parseInt of Integer wrapper class. It takes one parameter of string type and converts it to integer. So, we would need to convert char to string in order to use parseInt function.

Example : In this example, char variables are converted to int using parseInt method and valueOf method.

// Program to convert char to int in Java
public class Main {
    public static void main(String[] args) {
        // Initializing the variables
        char c = '9';
        char d = '5';

        // Converting char to int and printing
        int i = Integer.parseInt(String.valueOf(c));
        System.out.println("Converting char to int : " + i);

        i = Integer.parseInt(String.valueOf(d));
        System.out.println("Converting char to int : " + i);

    }
}

Output

Converting char to int : 9
Converting char to int : 5

 

Using Character.getNumericValue() method

In this approach, we will use getNumericValue() method which is a static method of Character wrapper class. This method returns an equivalent int value that the specified Unicode character represents.

Advertisement

Example : In this example, char variables are converted to int using getNumericValue method of Character class.

public class Main {
    public static void main(String[] args) {
        // Initializing the variables
        char c = '9';
        char d = '5';

        // Converting character to  int and printing
        int i = Character.getNumericValue(c);
        System.out.println("Converting character to int : " + i);

        i = Character.getNumericValue(d);
        System.out.println("Converting character to int : " + i);

    }
}

Output

Converting character to int : 9
Converting character to int : 5

 

Subtracting '0'

In this approach, we are simply subtracting the character '0' from the other character. That means, if we subtract '0' (ASCII equivalent 48) from any character let's say '2' (ASCII equivalent 50), we will get 2 as a result. Which means, '2'-'0'=2.

Example : In this example, char variables are converted to int by subtracting character '0' from the other character.

public class Main {
    public static void main(String[] args) {
        // Initializing the variables
        char c = '9';
        char d = '5';

        // Converting char to  int and printing
        int i = c - '0';
        System.out.println("Converting character to int : " + i);

        i = d - '0';
        System.out.println("Converting character to int : " + i);

    }
}

Output

Converting character to int : 9
Converting character to int : 5

 

Examples Showing conversion of char to int

Example 1 : Evaluating expression stored in String

In this example, we are evaluating the expression stored in the string after converting the numbers to equivalent int value.

public class Main {
    public static void main(String[] args) {
        // Initializing the variables
        String e = "2+3";
        char c;
        int first = 0, second = 0, result;

        // Iterating over the string
        for (int i = 0; i < e.length(); i++) {
            c = e.charAt(i);
            if (c >= '0' && c <= '9') {
                first = Integer.parseInt(String.valueOf(c));
                second = Integer.parseInt(String.valueOf(e.charAt(i + 2)));
            } else {
                result = first + second;
                System.out.println("Result of Expression " + e + " is " + result);
                break;
            }
        }

    }
}

Output

Result of Expression 2+3 is 5

 

Example 2 : Find the summation of numbers stored in the String

In this example, we are finding the summation of the numbers after converting the numbers to equivalent int value.

public class Main {
    public static void main(String[] args) {
        // Initializing the variables
        String e = "3455";
        int sum = 0;
        char c;
        for (int i = 0; i < e.length(); i++) {
            c = e.charAt(i);
            if (c >= '0' && c <= '9') {
                sum = sum + Integer.parseInt(String.valueOf(c));
            }
        }
        System.out.println("Summation of the numbers " + e + " is " + sum);
    }
}

Output

Summation of the numbers 3455 is 17

 

Example 3 : Find the product of numbers stored in the String

In this example, we are finding the product of the numbers after converting the numbers to equivalent int value.

public class Main {
    public static void main(String[] args) {
        // Initializing the variables
        String e = "1248";
        int p = 1;
        char c;
        for (int i = 0; i < e.length(); i++) {
            c = e.charAt(i);
            if (c >= '0' && c <= '9') {
                p = p * Integer.parseInt(String.valueOf(c));
            }
        }
        System.out.println("Product of the numbers " + e + " is " + p);
    }
}

Output

Product of the numbers 1248 is 64

 

Example 4 : Convert the multi digit numbers stored in String to int

In this example, we are converting the multi digit number stored in the string to its equivalent int value.

public class Main {
    public static void main(String[] args) {
        // Initializing the variables
        String s = "4588";
        char c;
        int i, num = 0, multiplier = 1;
        for (i = s.length() - 1; i >= 0; i--) {
            // Getting a character from the string
            c = s.charAt(i);

            // Converting character to int and multiplying with multiplier
            num = num + (Integer.parseInt(String.valueOf(c)) * multiplier);

            // Printing the intermediate values
            System.out.println("character is " + c);
            System.out.println("\n Now the number is " + num);

            // reset the multiplier
            multiplier = multiplier * 10;
        }
        // Printing the result
        System.out.println("Equivalent number is " + num);
    }
}

Output

character is 8

 Now the number is 8
character is 8

 Now the number is 88
character is 5

 Now the number is 588
character is 4

 Now the number is 4588
Equivalent number is 4588

 

Summary

The knowledge of converting char value to a int in Java is very useful while working on real time applications. In this tutorial, we covered four different approaches to convert char to int in Java. As per the requirement of an application, we can choose an appropriate approach for conversion. We learned in detail about this approaches with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on conversion of char value to a int value in Java.

 

References

Character class
Integer class

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X