我有String name = "admin"; 然后我做String char = name.substring(0,1); //char="a"
String name = "admin"
String char = name.substring(0,1); //char="a"
我想将转换char为ASCII值(97),如何在Java中执行此操作?
char
很简单的。只是把你char当成一个int。
int
char character = 'a'; int ascii = (int) character;
在你的情况下,你需要首先从String中获取特定的Character,然后进行转换。
String
Character
char character = name.charAt(0); // This gives the character 'a' int ascii = (int) character; // ascii is now 97.
尽管强制转换不是明确要求的,但是它提高了可读性。
int ascii = character; // Even this will do the trick.