java输出string的ascll码
时间: 2024-05-03 14:22:23 浏览: 7
可以使用以下代码输出字符串的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.print(ascii + " ");
}
```
输出结果为:72 101 108 108 111 32 87 111 114 108 100
相关问题
java String英文字母转ascll码
好的,我知道了。您可以使用Java中的charAt()和getNumericValue()方法将字符串中的每个字符转换为ASCLL码。以下是一个示例代码:
```
String str = "Hello World";
for (int i = 0; i < str.length(); i++) {
int ascii = Character.getNumericValue(str.charAt(i));
System.out.print(ascii + " ");
}
```
这将输出每个字符的ASCLL码。请注意,这只适用于英文字母,因为ASCLL码只适用于128个字符中的一部分。如果您需要更广泛的字符集,请考虑使用Unicode或UTF-8编码。
java实现ascll码转换
以下是 Java 实现 ASCII 码转换的示例代码:
```java
public class AsciiConverter {
public static void main(String[] args) {
char ch = 'A';
int ascii = (int) ch;
System.out.println("ASCII code of " + ch + " is: " + ascii);
int asciiCode = 65;
char character = (char) asciiCode;
System.out.println("Character for ASCII code " + asciiCode + " is: " + character);
}
}
```
输出:
```
ASCII code of A is: 65
Character for ASCII code 65 is: A
```
以上代码将字符 `A` 转换为 ASCII 码,并将 ASCII 码 `65` 转换为字符 `A`。你可以根据需要修改代码中的字符或 ASCII 码。