java 如何获得字符值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5746455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get character value?
提问by Nazerke
I have a piece of code like this...
我有一段这样的代码......
char c = 'a';
When I ask for Character.getNumericValue(c)
, it gives 10
as the output.
当我要求时Character.getNumericValue(c)
,它10
作为输出给出。
How can I swap this problem around so that 10
is the input and a
is the output?
我怎样才能交换这个问题,以便10
输入和a
输出?
回答by Michael Borgwardt
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:
请注意,10
在您的示例中, 不是 ASCII 代码,而是a
作为十六进制数字的值(或者更确切地说,任何大于 10 的基数中的数字)。要扭转这一点:
char c = Character.forDigit(10, 16);
Which you could have found by looking at the "see also" section in the API doc.
您可以通过查看 API 文档中的“另见”部分找到。
回答by Naftali aka Neal
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"
回答by Nick Banks
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 + "]");