1

Hi I've got this code for converting String to int manually.

int num = 0;
for(int i = 0; i < length(number); i++) {
    num *= 10;
    num += number.charAt(i) - '0';
    println("i :" + num);
}

Why do we subtract '0' at the end?

4
  • 2
    Does it have something to do with ASCII code? Commented May 22, 2014 at 10:01
  • It's because char is really a short interpreted in a different way. So in a way, you're calculating "distance from '0'" Commented May 22, 2014 at 10:01
  • 2
    see this example: int i = 'F' - 'A'; What is the value of F and A? What is the value of for example '3'? (hint: it's not 3) Commented May 22, 2014 at 10:05
  • 2
    Hint: it only works because the digits 0 to 9 have sequential ASCII codes (or Unicode codes) Commented May 22, 2014 at 10:07

4 Answers 4

6

The method .charAt(int position) returns a single character from your number string. Since your string contains a number you will receive a character which contains a digit (0 - 9). The next step would be to convert this character to an int.

a naive solution would be:

char digit = number.charAt(i);
if (digit == '0') {
    num += 0;
} else if (digit == '1') {
    num += 1;
}

But we can use the ASCII values of our characters to simplify this. Take a look at this ASCII table (only the columns 'Dec' and 'Chr'). You will see that the character 0 has in fact a value of 48. So if we substract 48 from our character we retrieve the correct value:

int num = digit - 48;

This can even be more simplified by directly placing the character which will be replaced by the compiler:

int num = digit - '0';

example:

character '4' has an ASCII value of 52. If we substract 48 we get 4 which is the wanted result.

Sign up to request clarification or add additional context in comments.

1 Comment

Does this approach faster than Integer.parseInt() ?
2

If number is String interpreted as number, the number.charAt(i) is a character from '0' —— '9', if you represent the chars as numbers, it would be 0 —— 9, moved to the code of '0' (something like '0' —— '0' + 9), so for getting the exact digit (as int: number), you should subtract the code of '0' from the char.

Comments

1

The char '0' equates to character 48.

'0' 48
'1' 49
'2' 50
...
'9' 57

Example:

If number.charAt(i) was '9' and 9' is character 57 then 57 - 48 = 9

Comments

0

ASCII value of '0' is 48, '1' is 49 and so on. Example: If a charactor variable "ch" is containing '5' now ASCII value of '5' is 53. If we want to perform mathematical operation '5' we have to convert it into int so we have to get it's integer value so we use ch-'0' (that means '5'-'0' that means 53-48 that is 5) by doing this we get integer value of '5'. Now we can perform mathematical operation on that. If you perform mathematical operation on '5', compiler will treat it as 53.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.