• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt on character arithmetic

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic