Can anyone knows why java Character.getNumericValue('a') returns 10 and C# char.GetNumericValue('a') returns -1?
-
Java part of the question http://stackoverflow.com/questions/13717898/java-character-literals-value-with-getnumericvalue and http://stackoverflow.com/questions/19388037/converting-characters-to-integers-in-java – Tunaki Aug 28 '16 at 15:47
-
1[Did you read the Javadoc](https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#getNumericValue(char))? – Andy Turner Aug 28 '16 at 15:48
-
Come on guys the question is simple why its in-consistent the same looking api in diff lang? downvoter please give a reason? – Imran Qadir Baksh - Baloch Aug 28 '16 at 15:50
-
Because language designers don't coordinate with eachother. – SLaks Aug 28 '16 at 15:51
-
3Your question is being downvoted because it's simply asking for documentation. – SLaks Aug 28 '16 at 15:51
-
@SLaks not documenation. I said the same looking api is completly different? can you expect that Math.Round returns diff result in diff language? – Imran Qadir Baksh - Baloch Aug 28 '16 at 15:52
-
1@user960567 you may not be *asking for* documentation, but the answer can be found there. They are methods in different languages; there is no particular reason for them to do exactly the same thing. – Andy Turner Aug 28 '16 at 15:57
-
1@user960567: Yes; as a matter of fact, `Math.round()` is also inconsistent. Different language designers will make different decisions. _Read the documentation_. https://msdn.microsoft.com/en-us/library/wyk4d9cy https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#round(double) – SLaks Aug 28 '16 at 15:57
-
For C#, the method _Converts the specified numeric Unicode character to a double-precision floating point number_. See the **documentation** here: https://msdn.microsoft.com/en-us/library/e7k33ktz(v=vs.110).aspx – Martin Aug 28 '16 at 15:58
-
**C#:** A character has an associated numeric value **if and only if it is a member of one of the following *UnicodeCategory* categories**: DecimalDigitNumber, LetterNumber, or OtherNumber. --- **Java:** The letters A-Z in their uppercase, lowercase, and full width variant 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. *As everybody has been saying: **READ THE DOCUMENTATION*** – Andreas Aug 28 '16 at 16:33
2 Answers
Different functions in different languages doing different things. The key difference here is that the C# function is specifically for numeric chars, whilst the Java function seems more general purpose.
Returns the int value that the specified Unicode character represents.
Converts a specified numeric Unicode character to a double-precision floating-point number. The numeric value of c if that character represents a number; otherwise, -1.0.
Perhaps you are looking for:
Converts the value of the specified Unicode character to the equivalent 32-bit signed integer.

- 17,286
- 4
- 46
- 89
If you want to bodge in a conversion from Java to C#, I wrote this helper function to do the same thing as the Java Character.getNumeric function. It's not clean and probably misses the corner cases, but it covers the basics. I didn't find a clean way of doing this with encoding/conversions in the .NET world, but one may well exist:
private int javaCharGetNumericValue(char c)
{
switch (c)
{
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case '0': return 0;
case 'A': case 'a': return 10;
case 'B': case 'b': return 11;
case 'C': case 'c': return 12;
case 'D': case 'd': return 13;
case 'E': case 'e': return 14;
case 'F': case 'f': return 15;
case 'G': case 'g': return 16;
case 'H': case 'h': return 17;
case 'I': case 'i': return 18;
case 'J': case 'j': return 19;
case 'K': case 'k': return 20;
case 'L': case 'l': return 21;
case 'M': case 'm': return 22;
case 'N': case 'n': return 23;
case 'O': case 'o': return 24;
case 'P': case 'p': return 25;
case 'Q': case 'q': return 26;
case 'R': case 'r': return 27;
case 'S': case 's': return 28;
case 'T': case 't': return 29;
case 'U': case 'u': return 30;
case 'V': case 'v': return 31;
case 'W': case 'w': return 32;
case 'X': case 'x': return 33;
case 'Y': case 'y': return 34;
case 'Z': case 'z': return 35;
default:
return -1;
}
}

- 4,968
- 1
- 18
- 12