ASCII和string转换

package com.isoftstone; import java.io.UnsupportedEncodingException; public class Convert { public static void main(String[] args) throws UnsupportedEncodingException { asciiToString();// ASCII转换为字符串 stringToAscii();// 字符串转换为ASCII码 } public static void asciiToString() {// ASCII转换为字符串 String s = "22307 35806 24555 20048";// ASCII码 String[] chars = s.split(" "); System.out.println("ASCII 汉字 \n----------------------"); for (int i = 0; i < chars.length; i++) { System.out.println(chars[i] + " " + (char) Integer.parseInt(chars[i])); } } public static void stringToAscii() {// 字符串转换为ASCII码 String s = "你好中国!";// 字符串 char[] chars = s.toCharArray(); // 把字符中转换为字符数组 System.out.println("\n\n汉字 ASCII\n----------------------"); for (int i = 0; i < chars.length; i++) {// 输出结果 System.out.println(" " + chars[i] + " " + (int) chars[i]); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzfywp.html