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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:34:12  来源:igfitidea点击:

how to get character value?

javacharacter-encoding

提问by Nazerke

I have a piece of code like this...

我有一段这样的代码......

char c = 'a'; 

When I ask for Character.getNumericValue(c), it gives 10as the output.

当我要求时Character.getNumericValue(c),它10作为输出给出。

How can I swap this problem around so that 10is the input and ais the output?

我怎样才能交换这个问题,以便10输入和a输出?

回答by Michael Borgwardt

Do realize that the 10in your example is not the ASCII code but the value of aas 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 + "]");