Convert a char to an int
We have two techniques for converting a single character to an integer type, as seen below:
1) Through Casting:
To convert a single character to its integer equivalent, we can utilize the casting operation. The ASCII equivalent of the character will be displayed in this scenario.
Consider the following conversion:
char a = 'A';
int num = (int) a;
In this example, the value of ‘num' will be 65, which is the ASCII equivalent of the character ‘A'.
If we wish to convert a number character to an integer value, we can perform the following operation.
char a = '2';
int num = a-48;
The ASCII value for 0 is 48 in this case. The second line, a-48, implicitly translates char a to its ASCII counterpart before subtracting it from 48 (50-48), yielding the integer value 2.