I have a piece of code like this...
char c = 'a';
When I ask for Character.getNumericValue(c)
, it gives 10
开发者_如何学运维as the output.
How can I swap this problem around so that 10
is the input and a
is the output?
Do realize that the 10
in your example is not the ASCII code but the value of a
as a hex digit (or rather, digit in any base greater than 10). To reverse that:
char c = Character.forDigit(10, 16);
Which you could have found by looking at the "see also" section in the API doc.
char c = 'a';
int i = 10;
System.out.println("Character c = [" + c + "], numeric valule = [" + (int)c + "]");
System.out.println("int i = [" + i + "], character valule = [" + (char)i + "]");
you can try:
int i = 97;
char c = (char) i; //should yield 'a';
System.out.println( "Integer " + i + " = Character " + c );
//outputs: "Integer 10 = Character a"
精彩评论