• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt on character arithmetic  RSS feed

 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The output for the above code is as follows

15
0
-41


How does -41 come out as result of third print statement?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:


The ASCII and Unicode value of the character '0' is 48.

And I think you should reread the docs for Character.getNumericValue(). I think you have a misconception about what it does.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
And I think you should reread the docs for Character.getNumericValue(). I think you have a misconception about what it does.


Jeff,

How did you come to this conclusion? The question wasn't directly related to that. Or didn't seem to be.

Henry
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does -41 come out as result of third print statement?

And what did you expect the result to be?

I suppose Mr Verdegan wanted to point OP to the difference between these two:

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Jeff Verdegan wrote:
And I think you should reread the docs for Character.getNumericValue(). I think you have a misconception about what it does.


Jeff,

How did you come to this conclusion? The question wasn't directly related to that. Or didn't seem to be.

Henry


The subject "character arithmetic" combined with the fact that he used that method and was confused about the results.
 
Angus Comber
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Rai wrote:

The output for the above code is as follows

15
0
-41


How does -41 come out as result of third print statement?


15 - I don't understand that. Hope someone might comment.

0 because Character.getNumericValue() function extracts the integer from the character. So '0' is of course zero.

'0' is ascii value 48 decimal (or 0x30) - see http://www.asciitable.com/

therefore 7 - 48 = -41

But I am puzzled with the 15 result.


EDIT

Just looked again at ascii chart. decimal 102 is character f. In hex this can be converted to decimal 15 - so there is that answer.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angus Comber wrote:
15 - I don't understand that. Hope someone might comment.


When the method is given a character that is a letter (meaning 'A' to 'Z' or 'a' to 'z'), it assumes that the number is for a base that has that letter. In this case, a character with an ASCII value of 102 is the letter 'f'. And for a base 16 number, or any base higher than base 16, the character 'f' represents the number 15.

Henry

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!