get char value in java

2020-02-11 23:42发布

How can I get the UTF8 code of a char in Java ? I have the char 'a' and I want the value 97 I have the char 'é' and I want the value 233

here is a table for more values

I tried Character.getNumericValue(a) but for a it gives me 10 and not 97, any idea why?

This seems very basic but any help would be appreciated!

8条回答
狗以群分
2楼-- · 2020-02-12 00:16

You can use the codePointAt(int index) method of java.lang.String for that. Here's an example:

"a".codePointAt(0) --> 97
"é".codePointAt(0) --> 233

If you want to avoid creating strings unnecessarily, the following works as well and can be used for char arrays:

Character.codePointAt(new char[] {'a'},0)
查看更多
老娘就宠你
3楼-- · 2020-02-12 00:16

This produces good result:

int a = 'a';
System.out.println(a); // outputs 97

Likewise:

System.out.println((int)'é');

prints out 233.

Note that the first example only works for characters included in the standard and extended ASCII character sets. The second works with all Unicode characters. You can achieve the same result by multiplying the char by 1. System.out.println( 1 * 'é');

查看更多
登录 后发表回答