我有字符串名称= "admin"; 然后我做String charValue = name.substring(0,1);/ / charValue = " "
我想将charValue转换为它的ASCII值(97),我如何在java中做到这一点?
我有字符串名称= "admin"; 然后我做String charValue = name.substring(0,1);/ / charValue = " "
我想将charValue转换为它的ASCII值(97),我如何在java中做到这一点?
当前回答
String str = "abc"; // or anything else
// Stores strings of integer representations in sequence
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray())
sb.append((int)c);
// store ascii integer string array in large integer
BigInteger mInt = new BigInteger(sb.toString());
System.out.println(mInt);
其他回答
public class Ascii {
public static void main(String [] args){
String a=args[0];
char [] z=a.toCharArray();
for(int i=0;i<z.length;i++){
System.out.println((int)z[i]);
}
}
}
String str = "abc"; // or anything else
// Stores strings of integer representations in sequence
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray())
sb.append((int)c);
// store ascii integer string array in large integer
BigInteger mInt = new BigInteger(sb.toString());
System.out.println(mInt);
如果您想将整个字符串转换为连接的ASCII值,那么您可以使用这个-
String str = "abc"; // or anything else
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray())
sb.append((int)c);
BigInteger mInt = new BigInteger(sb.toString());
System.out.println(mInt);
其中你将得到979899作为输出。
这要归功于它。
我只是把它复制在这里,这样其他人就方便了。
最简单的方法是:
对于整个字符串转换成ASCII:
public class ConvertToAscii{
public static void main(String args[]){
String abc = "admin";
int []arr = new int[abc.length()];
System.out.println("THe asscii value of each character is: ");
for(int i=0;i<arr.length;i++){
arr[i] = abc.charAt(i); // assign the integer value of character i.e ascii
System.out.print(" "+arr[i]);
}
}
}
输出结果为:
每个字符的asscii值为: 97 100 109 105 110 这里,abc.charAt(i)给出了String数组的单个字符: 当我们将每个字符赋值为整型时,编译器会进行类型转换,
arr[i] = (int) character //这里,每个单独的字符都是ascii值
但是,对于单个字符:
字符串名称= admin; asciiValue = (int) name.charAt(0);//字符'a' System.out.println (asciiValue);
String name = "admin";
char[] ch = name.toString().toCharArray(); //it will read and store each character of String and store into char[].
for(int i=0; i<ch.length; i++)
{
System.out.println(ch[i]+
"-->"+
(int)ch[i]); //this will print both character and its value
}