开发者

how to get character value?

开发者 https://www.devze.com 2023-02-28 11:35 出处:网络
I have a piece of code like this... char c = \'a\'; When I ask for Character.getNumericValue(c), it gives 10 开发者_如何学运维as the output.

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"
0

精彩评论

暂无评论...
验证码 换一张
取 消