Skip to content
Advertisement

Char variable minus char variable parse to Int in java

I know this is trivial, but I can’t find the proper explication. I have the following code

JavaScript

How does this parsing work?

Advertisement

Answer

As long as the character is a digit, you can get the equivalent int value by subtracting '0'. The ASCII coding for '0' is decimal 48, '1' is decimal 49, etc.

So '8' - '0' = 56 - 48 = 8;

For your number, you can parse the entire string like this (assuming all the characters are digits, otherwise the result wouldn’t make sense).

JavaScript

Prints

JavaScript

Explanation

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement