欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

字符串与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();  
    }  

}
相关标签: ASCII

上一篇: 爬虫之scrapy(二)

下一篇: