6
    int x = Character.getNumericValue('A');
    System.out.println(" the value of x is:  " + x); // prints 10

I am looking for a method that takes in somemethod(10) and returns 'A'. Does such a method exist in java ?

JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
  • 3
    `getNumericValue` doesn't produce a one-to-one mapping; a unique inverse doesn't always exist. – Oliver Charlesworth Jun 07 '14 at 19:59
  • > 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. – Anubian Noob Jun 07 '14 at 20:00
  • System.out.println(" the value of x is: " + Character.getNumericValue('A')); -- it is giving me 10 – JavaDeveloper Jun 07 '14 at 20:02
  • @OliCharlesworth Thanks, too early in my morning :| – user2864740 Jun 07 '14 at 20:03
  • 1
    Consider `Character.forDigit(int, int)` that the docs for `getNumericValue()` also link to. Using `System.out.println(Character.forDigit(10, 11));` will print `a` (although I don't know what the purpose of *radix* is, you'll have to find this out yourself. http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#forDigit(int,%20int) – Jeroen Vannevel Jun 07 '14 at 20:05
  • But to answer your implied question, `Integer.toString(intVal, radix)` would be the approximate reverse. – Hot Licks Jun 07 '14 at 20:05
  • (Though now that it's pointed out `Character.forDigit` is probably a better choice.) – Hot Licks Jun 07 '14 at 20:06
  • @jeroen base-11 math? Presumably op wants 16 for hex. – Elliott Frisch Jun 07 '14 at 20:16
  • @ElliottFrisch: yeah, I found it after the 5minute grace period. The requirement for the radix was that it is bigger than the value itself. Value `10` will return `a` with any radix from `11` to `36`. – Jeroen Vannevel Jun 07 '14 at 20:18
  • Character.forDigit is problematic if the code point cannot be represented by a single char. – Brett Okken Jun 07 '14 at 20:20

1 Answers1

2

As stated by Anubian Noob and Jeroen Vannevel, Character.GetNumericValue('A') = 10, Character.GetNumericValue('a') = 10 and `Character.forDigit(10, 36) ='a'.

So IMHO, what is closest of what you ask would be

Character fromNumericValue(int x) {
    if ((x < 0) || (x > 35)) {
        throw new IllegalArgumentException();
    }
    return Character.toUpperCase(Character.forDigit(x, 36));
}

It works for numbers between 0 and 9, returning chars '0' to '9', and for numbers between 10 and 35 returning letters 'A' to 'Z'. But I cannot imagine how you will use it.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • If you are only interested in 0-9 A-Z this will work. However, if you want to use code points which require a surrogate pair, this will not work. http://stackoverflow.com/questions/5903008/what-is-a-surrogate-pair-in-java – Brett Okken Jun 08 '14 at 13:45
  • @BrettOkken Beware, OP asked about `getNumericValue` which is **not** the same as code point. For example numeric value so 'a' and 'A' is 10 whereas code point of 'A' is 65 and code point of 'a' is 97 – Serge Ballesta Jun 08 '14 at 14:57