import java.io.UnsupportedEncodingException; public class T { public static void main(String[] args) throws UnsupportedEncodingException { t1();//ASCII转换为字符串 t2();//字符串转换为ASCII码 } public static void t1(){//ASCII转换为字符串 String s="22307 35806 24555 20048";//ASCII码 String[]chars=s.split(" "); System.out.println("ASCII 汉字 ----------------------"); for(int i=0;i<chars.length;i++){ System.out.println(chars[i]+" "+(char)Integer.parseInt(chars[i])); } } public static void t2(){//字符串转换为ASCII码 String s="新年快乐!";//字符串 char[]chars=s.toCharArray(); //把字符中转换为字符数组 System.out.println(" 汉字 ASCII ----------------------"); for(int i=0;i<chars.length;i++){//输出结果 System.out.println(" "+chars[i]+" "+(int)chars[i]); } } }
转载:http://blog.chinaunix.net/uid-20447986-id-1945654.html