java 怎么把字符串转化为ascii
import java.io.UnsupportedEncodingException;
public class StringToAscii {
public static void main(String[] args) throws UnsupportedEncodingException {
AsciiToString();//ASCII转换为字符串
StringToAscii();//字符串转换为ASCII码
}
public static void AsciiToString(){//ASCII转换为字符串
String s="97 98 99";//ASCII码
String[]chars=s.split(" ");
System.out.println("ASCII TO STRING \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="abc";//字符串
char[]chars=s.toCharArray(); //把字符中转换为字符数组
System.out.println("STRING TO ASCII\n----------------------");
for(int i=0;i<chars.length;i++)
{
System.out.println(chars[i]+" "+(int)chars[i]);
}
}
}