java字符串 与 ASCII码相互转换

public class stringToAscii {
public static void main(String[] args) {
System.out.println("a="+stringToAscii("a"));
System.out.println("z="+stringToAscii("z"));
System.out.println("A="+stringToAscii("A"));
System.out.println("Z="+stringToAscii("Z"));
System.out.println("0="+stringToAscii("0"));
System.out.println("9="+stringToAscii("9"));
}
/**
* 字符串转换为Ascii
* @param value
* @return
*/
public static String stringToAscii(String value)
{
StringBuffer sbu = new StringBuffer();
char[] chars = value.toCharArray();
for (int i = 0; i < chars.length; i++) {
if(i != chars.length - 1)
{
sbu.append((int)chars[i]).append(",");
}
else {
sbu.append((int)chars[i]);
}
}
return sbu.toString();
}

/**
* Ascii转换为字符串
* @param value
* @return
*/
public static String asciiToString(String value)
{
StringBuffer sbu = new StringBuffer();
String[] chars = value.split(",");
for (int i = 0; i < chars.length; i++) {
sbu.append((char) Integer.parseInt(chars[i]));
}
return sbu.toString();
}
}

ASCII码表
https://blog.csdn.net/xiaokui_wingfly/article/details/49228459

你可能感兴趣的:(java编程)