问题
我有String name = "admin";
然后我做String char = name.substring(0,1); //char="a"
我想将288613348转换为它的ASCII值(97),我怎么能在java中这样做?
#1 热门回答(196 赞)
很简单。刚刚投射你的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.
#2 热门回答(42 赞)
只是一种不同的方法
String s = "admin";
byte[] bytes = s.getBytes("US-ASCII");
bytes[0]
将表示一个..的ascii,从而表示整个数组中的其他字符。
#3 热门回答(17 赞)
而不是这个:
String char = name.substring(0,1); //char="a"
你应该使用charAt()
方法。
char c = name.charAt(0); // c='a'
int ascii = (int)c;