编程:输入a-z,A-Z中的字母组成字符串,输出下一个字母,如:
azAZ 输出:baBA
-----------------------------------
public class TestASCII { public static void main(String[] args) {
String str="azAZ";
String s = stringToAscii(str);
System.out.println("str 转化为ascii:"+s);
String s1 = asciiToString(s);
System.out.println("str 的ascii加1 后:"+ s1);
}
public static String stringToAscii(String str)
{
StringBuffer sbu = new StringBuffer();
char[] chars = str.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]);
}
}
String s = sbu.toString();
return s;
}
public static String asciiToString(String str){
StringBuffer sb = new StringBuffer();
String[] chars = str.split(",");
// System.out.println("chars.toString():"+chars.toString());
for (int i = 0; i < chars.length; i++) {
String aChar = chars[i];
int i1 = Integer.parseInt(aChar);
i1+=1;
if(i1==91 ){
i1=65;
}
if(i1== 123){
i1=97;
}
sb.append((char)i1);
}
String asciiToString=sb.toString();
return asciiToString ;
}
}
---------------------------------------------------------
总结:
String str="azAZ";
--------------------------------
1 字符得到ascii码, 如
char[] chars = str.toCharArray();
(int)chars[i]》 >>>>>>得到的就是ascii码
--------------------------------
2:ascii 转字母。
String[] chars = str.split(",");
String aChar = chars[i];
(char)Integer.parseInt(aChar);>>>>>得到的就是ascii码对应的字母。