可以使用Java的charAt()方法和Integer类的valueOf()方法来实现将字符串转换为ASCII的数值。以下是一个示例代码:

public class StringToAscii {
    public static void main(String[] args) {
        String str = "Hello World!";
        int[] asciiValues = convertToAscii(str);
        
        System.out.println("String: " + str);
        System.out.print("ASCII values: ");
        for (int value : asciiValues) {
            System.out.print(value + " ");
        }
    }
    
    public static int[] convertToAscii(String str) {
        int[] asciiValues = new int[str.length()];
        
        for (int i = 0; i < str.length(); i++) {
            asciiValues[i] = (int) str.charAt(i);
        }
        
        return asciiValues;
    }
}

输出结果:

String: Hello World!
ASCII values: 72 101 108 108 111 32 87 111 114 108 100 33

该代码将字符串"Hello World!"转换为对应的ASCII值并输出。

请用java语言将字符串换成ascii的数值

原文地址: https://www.cveoy.top/t/topic/jbIe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录