String转ascii码

思路

String -> char[] -> ASCII集合 -> String

代码

String s1 = "123";
String s2 = "hhh";
String s3 = "哈哈";

char[] ch1 = s1.toCharArray();
char[] ch2 = s2.toCharArray();
char[] ch3 = s3.toCharArray();

StringBuilder stringBuilder1 = new StringBuilder();
StringBuilder stringBuilder2 = new StringBuilder();
StringBuilder stringBuilder3 = new StringBuilder();

String str1 = "",str2 = "",str3 = "";
for (int i = 0; i < ch1.length; i++){
    stringBuilder1.append(Integer.valueOf(ch1[i]).intValue()).append(",");
    str1 = stringBuilder1.toString();
}
for (int i = 0; i < ch2.length; i++){
    stringBuilder2.append(Integer.valueOf(ch2[i]).intValue()).append(",");
    str2 = stringBuilder2.toString();
}
for (int i = 0; i < ch3.length; i++){
    stringBuilder3.append(Integer.valueOf(ch3[i]).intValue()).append(",");
    str3 = stringBuilder3.toString();
}

System.out.println(s1 + " -> " + str1);
System.out.println(s2 + " -> " + str2);
System.out.println(s3 + " -> " + str3);       

运行结果

 

 

你可能感兴趣的:(Android)