很简单的。只是把你
char当成一个
int。
char character = 'a'; int ascii = (int) 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.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)