Java Char to Integer Conversion in C Programming and Java

please click here for more wordpress cource

In C Programming, you can convert a char to an integer using the ASCII value of the character. Since each character is represented by an ASCII value, you can use the built-in int function to convert a char to an integer. Here is an example:

char c = '5';
int i = c - '0';
printf("%d\n", i); // Output: 5

In this example, the char c is initialized to the character ‘5’. To convert c to an integer, we subtract the ASCII value of ‘0’ from c. This is because the ASCII value of ‘0’ is 48, and the ASCII values of ‘1’ through ‘9’ are 49 through 57, respectively. Therefore, subtracting ‘0’ from any digit character will give its integer equivalent.

In Java, you can use the Character and Integer classes to convert a char to an integer. Here is an example:

char c = '5';
int i = Character.getNumericValue(c);
System.out.println(i); // Output: 5

In this example, the char c is initialized to the character ‘5’. To convert c to an integer, we use the getNumericValue() method of the Character class, which returns the integer value of the specified character.

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *