Java程序将字符串转为unicode的方法

来源:互联网 发布:sql数据库查询语句大全 编辑:程序博客网 时间:2024/06/05 18:25
private static String convert(String str) {        String tmp;        StringBuffer sb = new StringBuffer(1000);        char c;        int i, j;        sb.setLength(0);        for (i = 0; i < str.length(); i++) {            c = str.charAt(i);            if (c > 255) {                sb.append("\\u");                j = (c >>> 8);                tmp = Integer.toHexString(j);                if (tmp.length() == 1)                    sb.append("0");                sb.append(tmp);                j = (c & 0xFF);                tmp = Integer.toHexString(j);                if (tmp.length() == 1)                    sb.append("0");                sb.append(tmp);            } else {                sb.append(c);            }        }        return (new String(sb));    }

0 0