在java中将字符转换为ASCII数值

我有String name = "admin";
那么我做String char = name.substring(0,1); //char="a" String char = name.substring(0,1); //char="a"

我想将char转换为它的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. 

尽pipe投射不是明确要求的,但是其提高了可读性。

 int ascii = character; // Even this will do the trick. 

只是一个不同的方法

  String s = "admin"; byte[] bytes = s.getBytes("US-ASCII"); 

bytes[0]将表示ASCII的ascii,因此表示整个数组中的其他字符。

而不是这个:

 String char = name.substring(0,1); //char="a" 

你应该使用charAt()方法。

 char c = name.charAt(0); // c='a' int ascii = (int)c; 

如果你想把整个string转换成连接的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作为输出。

信用这个 。

我只是在这里复制它,这样可以方便其他人。

旨在说明如何做到这一点的几个答案都是错误的,因为Java字符不是ASCII字符。 Java使用Unicode字符的多字节编码。 Unicode字符集是ASCII的超集。 所以在Javastring中可以有不属于ASCII的字符。 这些字符没有ASCII数字值,所以询问如何获取Java字符的ASCII数值是无法parsing的。

为什么你要这样做? 你打算怎么做的价值?

如果您想要数字值,那么您可以将Javastring转换为ASCIIstring, 真正的问题是“如何将Javastring编码为ASCII”。 为此,使用对象StandardCharsets.US_ASCII。

将char转换为int。

  String name = "admin"; int ascii = name.toCharArray()[0]; 

另外:

 int ascii = name.charAt(0); 

这很简单,得到你想要的字符,并将其转换为int。

 String name = "admin"; int ascii = name.charAt(0); 

我知道这已经有几种forms的答案,但这里是我的代码,看看所有的字符。

这是代码,从类开始

 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 } } 

}

 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); 
 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 } 
 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]); } } } 

正如@Rededwald所指出的那样,Java的Unicode不会迎合所有字符来获取ASCII值。 正确的方法(Java 1.7+)如下所示:

 byte[] asciiBytes = "MyAscii".getBytes(StandardCharsets.US_ASCII); String asciiString = new String(asciiBytes); //asciiString = Arrays.toString(asciiBytes) 

只需将char转换为int即可。

 char character = 'a'; int number = (int) character; 

number的值将是97。

你可以用这个代码检查ASCII码。

 String name = "admin"; char a1 = a.charAt(0); int a2 = a1; System.out.println("The number is : "+a2); // the value is 97 

如果我错了,道歉。

如果你想要一个string中的所有字符的ASCII值。 你可以使用这个:

 String a ="asdasd"; int count =0; for(int i : a.toCharArray()) count+=i; 

如果你想要一个string中的单个字符的ASCII你可以去:

 (int)a.charAt(index);