• Post Reply Bookmark Topic Watch Topic
  • New Topic

Character.getNumericValue()  RSS feed

 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does Character.getNumericValue do?
According to the definition that it " Returns the Unicode numeric value of the character as a nonnegative integer "
I thought it would return the Unicode Value for any given character.
But,
System.out.println(Character.getNumericValue('a'));
gives
10 . why not 61?
Jennifer
 
Thomas Bigbee
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was a bit confused at first myself - but if you read the method definition in the api spec - you will notice that it is a (normal?) occurance.
The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have numeric values from (((10 through 35))).
This is independent of the Unicode specification, (((which does not assign numeric values to these char values)))
------------------
 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry. But I did not get you.
I read in the API Docs that getNumericValue() returns the Unicode numeric value of a character. Is that wrong ?
So how come 10 is the numeric value of 'a'. In what context, does it represent 'a'? The only thing I can think of is that 'a' is the hexadecimal representation of 10. But that doesnt go beyond f.
Could u please elaborate ?
 
Thomas Bigbee
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are correct but you need to read the api docs very carefully
The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A') forms have numeric values from ***** 10 through 35. ***** This is independent of the Unicode specification, ***** which does not assign numeric values to these char values. *****
I have put stars around the parts which need careful notice. It would seem that Character.getNumericValue() is strange for ascii chars
Try the below code and see what you get
System.out.println("the ascii 'A' char is: "+ Character.getNumericValue('\u0041'));
System.out.println("the ascii 'Z' char is: "+ Character.getNumericValue('\u005A'));
'A' = 10;
'Z' == 35;
Hope that helps, Tom

------------------
 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for ur patient explanation.
 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Bigbee wrote:you are correct but you need to read the api docs very carefully
The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A') forms have numeric values from ***** 10 through 35. ***** This is independent of the Unicode specification, ***** which does not assign numeric values to these char values. *****
I have put stars around the parts which need careful notice. It would seem that Character.getNumericValue() is strange for ascii chars
Try the below code and see what you get
System.out.println("the ascii 'A' char is: "+ Character.getNumericValue('\u0041'));
System.out.println("the ascii 'Z' char is: "+ Character.getNumericValue('\u005A'));
'A' = 10;
'Z' == 35;
Hope that helps, Tom

------------------

I believe the ASCII value for A is decimal 65, verify it here: http://www.asciitable.com I don't know what the value produced by Character.getNumericValue('\u0041') represents but it is not a the ASCII value.
 
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
Ivan Turner wrote:
Thomas Bigbee wrote:you are correct but you need to read the api docs very carefully
The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A') forms have numeric values from ***** 10 through 35. ***** This is independent of the Unicode specification, ***** which does not assign numeric values to these char values. *****
I have put stars around the parts which need careful notice. It would seem that Character.getNumericValue() is strange for ascii chars
Try the below code and see what you get
System.out.println("the ascii 'A' char is: "+ Character.getNumericValue('\u0041'));
System.out.println("the ascii 'Z' char is: "+ Character.getNumericValue('\u005A'));
'A' = 10;
'Z' == 35;
Hope that helps, Tom

------------------

I believe the ASCII value for A is decimal 65, verify it here: http://www.asciitable.com I don't know what the value produced by Character.getNumericValue('\u0041') represents but it is not a the ASCII value.


If it isn't obvious, the getNumericValue() method returns the numberic value for the character -- assuming the number is using a base that uses that character.

For example, for hexadecimal numbers (base 16), "a" is 10, "b" is 11, etc. And obviously, for "z" to be used, you will need a base of 36 or higher.

Henry
 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Bigbee is saying that those values in his example are ascii values. They are not!!! If you look up the unicode value for uppercase A, it is not 10 either. BTW, I am very well aware 10 base 16 is 16 and a = 10, b = 11, ...
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Turner wrote: . . . If you look up the unicode value for uppercase A, it is not 10 either. BTW, I am very well aware 10 base 16 is 16 and a = 10, b = 11, ...
. . . and 0x41 = 65decimal. So the letter ‘A’ comes out as 0x41 when you print its hexadecimal value.

Yes, 10 base 16 is what you get from ‘A’/‘a’ and 10 is what the numeric value method returns. That means you can choose a nice round number (3 dozen) and use that as the maximum permitted radix for this sort of method. Did they choose 36 because it is a nice round number, or because you can use letters up to z or both?And lo and behold, the numeric value for ‘z’/‘Z’ is 35. 36 + 35 = 71.
So Thomas Bigbee was, all those years ago, 100% correct. He said the numeric value is different from the ASCII value (which is the same as the newer Unicode value for ASCII characters). Note the numeric value method may return -1 or -2 under some circumstance.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!