字符串与ASCII的转换
程序员文章站
2024-03-17 14:55:58
...
public class ASCIIUtil {
public static void main(String[] args) {
System.out.println(stringToAscii("hello嗨喽"));
System.out.println(asciiToString("104,101,108,108,111,21992,21949"));
}
public static String stringToAscii(String value) {
StringBuffer stringBuffer = new StringBuffer();
char[] chars = value.toCharArray();
for (char c : chars) {
stringBuffer.append((int) c).append(",");
}
return stringBuffer.toString().substring(0, stringBuffer.length() - 1);
}
public static String asciiToString(String value)
{
StringBuffer stringBuffer = new StringBuffer();
String[] chars = value.split(",");
for(String c : chars){
stringBuffer.append((char) Integer.parseInt(c));
}
return stringBuffer.toString();
}
}
上一篇: 爬虫之scrapy(二)