我有字符串名称= "admin"; 然后我做String charValue = name.substring(0,1);/ / charValue = " "
我想将charValue转换为它的ASCII值(97),我如何在java中做到这一点?
我有字符串名称= "admin"; 然后我做String charValue = name.substring(0,1);/ / charValue = " "
我想将charValue转换为它的ASCII值(97),我如何在java中做到这一点?
非常简单。只需将char类型转换为int类型。
char character = 'a';
int ascii = (int) character;
在本例中,您需要首先从String中获取特定的字符,然后强制转换它。
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.
只需将char类型转换为int类型。
char character = 'a';
int number = (int) character;
number的值为97。
而不是这样:
String char = name.substring(0,1); //char="a"
您应该使用charAt()方法。
char c = name.charAt(0); // c='a'
int ascii = (int)c;
将char型转换为int型。
String name = "admin";
int ascii = name.toCharArray()[0];
另外:
int ascii = name.charAt(0);
如果您想将整个字符串转换为连接的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作为输出。
这要归功于它。
我只是把它复制在这里,这样其他人就方便了。
只是不同的方法
String s = "admin";
byte[] bytes = s.getBytes("US-ASCII");
字节[0]将表示一个..也就是整个数组中的其他字符。
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 CheckChValue { // Class name
public static void main(String[] args) { // class main
String name = "admin"; // String to check it's value
int nameLenght = name.length(); // length of the string used for the loop
for(int i = 0; i < nameLenght ; i++){ // while counting characters if less than the length add one
char character = name.charAt(i); // start on the first character
int ascii = (int) character; //convert the first character
System.out.println(character+" = "+ ascii); // print the character and it's value in ascii
}
}
}
你可以用这段代码检查ASCII的数字。
String name = "admin";
char a1 = a.charAt(0);
int a2 = a1;
System.out.println("The number is : "+a2); // the value is 97
如果我错了,我道歉。
几个旨在说明如何做到这一点的答案都是错误的,因为Java字符不是ASCII字符。Java使用Unicode字符的多字节编码。Unicode字符集是ASCII的超集。因此,Java字符串中可能存在不属于ASCII的字符。这样的字符没有ASCII数字值,因此询问如何获得Java字符的ASCII数字值是无法回答的。
但你为什么要这么做?你要怎么处理这个值呢?
如果你想要数值,这样你就可以将Java字符串转换为ASCII字符串,真正的问题是“我如何将Java字符串编码为ASCII”。为此,使用StandardCharsets.US_ASCII对象。
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
}
如果你想要一个字符串中所有字符的ASCII值。你可以用这个:
String a ="asdasd";
int count =0;
for(int i : a.toCharArray())
count+=i;
如果你想要字符串中单个字符的ASCII码,你可以使用:
(int)a.charAt(index);
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]);
}
}
}
正如@Raedwald指出的那样,Java的Unicode并不能满足所有字符获取ASCII值的需求。正确的方法(Java 1.7+)如下:
byte[] asciiBytes = "MyAscii".getBytes(StandardCharsets.US_ASCII);
String asciiString = new String(asciiBytes);
//asciiString = Arrays.toString(asciiBytes)
我尝试同样的事情,但最好和最简单的解决方案是使用charAt和访问索引,我们应该创建一个[128]大小的整数数组。
String name = "admin";
int ascii = name.charAt(0);
int[] letters = new int[128]; //this will allocate space with 128byte size.
letters[ascii]++; //increments the value of 97 to 1;
System.out.println("Output:" + ascii); //Outputs 97
System.out.println("Output:" + letters[ascii]); //Outputs 1 if you debug you'll see 97th index value will be 1.
如果你想显示完整字符串的ascii值,你需要这样做。
String name = "admin";
char[] val = name.toCharArray();
for(char b: val) {
int c = b;
System.out.println("Ascii value of " + b + " is: " + c);
}
在这种情况下,你的输出将是: a的Ascii值为:97 d的Ascii值为:100 m的Ascii值为:109 i的Ascii值为:105 n的Ascii值是:110
最简单的方法是:
对于整个字符串转换成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);
或者你可以使用流API为一个字符或从Java 1.8开始的字符串:
public class ASCIIConversion {
public static void main(String[] args) {
String text = "adskjfhqewrilfgherqifvehwqfjklsdbnf";
text.chars()
.forEach(System.out::println);
}
}
使用Java 9 => String.chars()
String input = "stackoverflow";
System.out.println(input.chars().boxed().collect(Collectors.toList()));
输出- [115,116,97,99,107,111,118,101,114,102,108,111,119]
为此,我们可以直接使用String类
input.codePointAt(index);
我想再给出一个建议,以获得整个字符串转换为相应的ascii码,使用java 8 例如:“abcde”~“979899100101”。
String input = "abcde";
System.out.println(
input.codePoints()
.mapToObj((t) -> "" + t)
.collect(joining()));
不使用额外int变量的一行解决方案:
String name = "admin";
System.out.println((int)name.charAt(0));