0

Hi I want the opposite function of getNumericValue

int i = Character.getNumericValue('A');
if('A' == Character.someFunction(i)){
      System.out.println("hooray");
}

I have tried "Character.forDigit" but this seems to be completely wrong.

Am new to java so please help.

Akash Deshpande
  • 2,583
  • 10
  • 41
  • 82

3 Answers3

2

To convert between char and int you can use typecasting. For example:

char myChar = (char) 65;
System.out.println(myChar);

will result in A. Hope that helps!

westonkd
  • 96
  • 9
2

The opposite is Character.forDigit

if(Character.forDigit(Character.getNumericValue('b'), Character.MAX_RADIX) == 'b') {
    // true!
}
if(Character.forDigit(Character.getNumericValue('B'), Character.MAX_RADIX) == 'b') {
    // true!
}


if(Character.getNumericValue('B') == Character.getNumericValue('b')) {
    // true!
}
if((int)('B') == (int)'b') {
    // false
}

Although given your question I think your looking for the actual ASCII char code for the letter.

Read this Java Character literals value with getNumericValue() post to see more information about Character.getNumericValue

Community
  • 1
  • 1
ug_
  • 11,267
  • 2
  • 35
  • 52
0

Character.getNumericValue('A') convert 'A' into unicode representation of the char. Probably you don't want to use that.

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.

Petar Butkovic
  • 1,820
  • 15
  • 14