Char to Int Java Example
1. Introduction
This article will demonstrate the char to int Java conversion. In Java, char data type is single 16 bit unicode character and int data type is 32 bit signed two’s complement integer. To convert one data type to another, type casting is required. Type casting can be of two types: implicit type casting and explicit type casting. Since char is smaller data type compared to int, this conversion can be achieved through implicit conversion, or in other words, we dont need to use type casting for char to int conversion. Let’s look at commonly used methods in projects and real time for conversion.
2. Implicit conversion of Char to Int
Below code will explain how character values stored in char datatype will be converted to equivalent ASCII values by casting it to int datatype in java. As explained earlier,char can be implicitly converted to integer since char is 16 bits and integer is 32 bit. This process of implicit conversion is also called automatic conversion.
Example1
public class CharToInt{ public static void main(String args[]){ //Character stored in char datatype char charLiteral_1 = 'c'; char charLiteral_2 = 'C'; char charLiteral_3 ='9'; //ASCII value of char to int in int datatype int numLiteral_1 = charLiteral_1; int numLiteral_2 = charLiteral_2; int numLiteral_3 = charLiteral_3; System.out.println("ASCII value of char "+charLiteral_1+ " is: "+numLiteral_1); System.out.println("ASCII value of char "+charLiteral_2+ " is: "+numLiteral_2); System.out.println("ASCII value of char "+charLiteral_3+ " is: "+numLiteral_3); } }
Output
ASCII value of char c is: 99 ASCII value of char C is: 67 ASCII value of char 9 is: 57
Above example shows if character values is implicitly casted to int, then it will return ASCII value for that character. Each character literal which is from A-Z,a-z and 0-9 has unique ASCII values which is of integer.Implicitly casting these literals returns unique integer. Since these ASCII values is not very large we can cast it to short dataype also.
3. Character.getNumericValue() to convert char to int in Java
Java provides support to convert character to int using its own API like Character.getNumericValue() and Integer.parseInt(). Below examples shows use of Character.getNumericValue(). getNumericValue() is a static method present in java.lang.Character class for which return type is equivalent integer ASCII value for character literal passed as an arguement.
Example2
public class CharToInt{ public static void main(String args[]){ char charLiteral_1 = 'Z'; //Use of Character.getNumericValue int numLiteral_1 = Character.getNumericValue(charLiteral_1); System.out.println("ASCII value of char "+charLiteral_1+ " is: "+numLiteral_1); } }
Output
ASCII value of char Z is: 35
4. Integer.parseInt() for Char to Int conversion
Another common API used for char to int conversion is Integer.parseInt() which belongs to java.lang.Integer class. parseInt() is also a static method which takes string literal as an arguement. Character literal is passed inside String.valueOf() which converts character to string and finally returns integer. Arguement passed inside String.valueOf() is parsed as signed decimal integer.The characters in the string must be all decimal digits. The resulting integer value is given exactly as if arguement was given. It will throw NumberFormatException if arguement is not of required format.
Example3
public class CharToInt{ public static void main(String args[]){ char charLiteral_2 = '5'; //Use of Integer.parseInt() int numLiteral_2 = Integer.parseInt (String.valueOf(charLiteral_2)); System.out.println("ASCII value of char "+charLiteral_2+ " is: "+numLiteral_2); } }
Output
ASCII value of char 5 is: 5
4. Char to Int Java – Conclusion
As we see, the main purpose of casting character values to integer is to fetch its ASCII values. Character array can be iterated in a loop using ASCII values. Integer values for char can be considered as an identifier which can be useful while iterating characters or doing some kind of logical operations in it.
5. Download the Source Code
That was a Char to Int Java Example.
You can download the full source code of this example here: Char to Int Java Example