Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Character literals value with getNumericValue()

Why do I get the same results for both upper- and lowercase literals? For instance:

char ch1 = 'A';
char ch2 = 'a';
char ch3 = 'Z';
char ch4 = 'z';

print("ch1 -- > " + Integer.toBinaryString(Character.getNumericValue(ch1)));
print("ch2 -- > " + Integer.toBinaryString(Character.getNumericValue(ch2)));
print("ch3 -- > " + Integer.toBinaryString(Character.getNumericValue(ch3)));
print("ch4 -- > " + Integer.toBinaryString(Character.getNumericValue(ch4)));

As results I get:

ch1 -- > 1010
ch2 -- > 1010
ch3 -- > 100011
ch4 -- > 100011

And don't really see the difference between 'A' and 'a'. Even if I use character literals in UTF form (\u0041 for 'A' and \u0061 for 'a') I do get the same results.

like image 448
Xentatt Avatar asked Dec 05 '12 06:12

Xentatt


People also ask

What is character getNumericValue in Java?

getNumericValue(char ch) Returns the int value that the specified Unicode character represents. static int. getNumericValue(int codePoint) Returns the int value that the specified character (Unicode code point) represents.

What does getNumericValue do?

GetNumericValue(String, Int32)Converts the numeric Unicode character at the specified position in a specified string to a double-precision floating point number.

How do I find the value of a char in a number?

If the char variable contains an int value, we can get the int value by calling Character. getNumericValue(char) method. Alternatively, we can use String. valueOf(char) method.


2 Answers

It's behaving exactly as documented:

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.

Basically this means that when parsing hex (say), 0xfa == 0xFA, as you'd expect.

I'd only expect case to matter when using something like base64.

like image 151
Jon Skeet Avatar answered Jan 03 '23 10:01

Jon Skeet


Judging from the commentary, you're actually looking for the codepoints of the characters, rather than their numeric value, so I'll just isolate that into an answer. The getNumericValue() function returns what the character means as a number when interpreting its glyph, it does not return the codepoint of a character. For instance, getNumericValue('5') returns 5 as an int, not the codepoint of 5.

To use the codepoints, just use your variables or the char literals as they are. char is a numeric datatype. For instance, System.out.println((int)'a'); will print 65, quite simply.

like image 24
Dolda2000 Avatar answered Jan 03 '23 12:01

Dolda2000