java 取字符的ascii_如何用java获得字符串的ASCII值
时间: 2024-01-27 07:03:12 浏览: 98
Java中输出字符的ASCII值实例
您可以使用Java中的`charAt()`和`(int)`方法来获取字符串中每个字符的ASCII值。
例如,以下代码将打印字符串`str`中每个字符的ASCII值:
```java
String str = "Hello, world!";
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
int ascii = (int) c;
System.out.println("ASCII value of " + c + " is " + ascii);
}
```
输出结果如下:
```
ASCII value of H is 72
ASCII value of e is 101
ASCII value of l is 108
ASCII value of l is 108
ASCII value of o is 111
ASCII value of , is 44
ASCII value of is 32
ASCII value of w is 119
ASCII value of o is 111
ASCII value of r is 114
ASCII value of l is 108
ASCII value of d is 100
ASCII value of ! is 33
```
阅读全文