Java字符串轉unicode

從別人處copy過來的,實用,備用

    /**
     * 字符串轉unicode
     */
    public static String convert(String str) {
        str = (str == null ? "" : 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);
            sb.append("\\u");
            System.out.println(c);
            j = (c >>> 8); // 取出高8位
            System.out.println("j = " + j);
            tmp = Integer.toHexString(j);
            System.out.println(tmp);
            if (tmp.length() == 1)
                sb.append("0");
            sb.append(tmp);
            j = (c & 0xFF); // 取出低8位
            System.out.println("j = " + j);
            tmp = Integer.toHexString(j);
            System.out.println(tmp);
            if (tmp.length() == 1)
                sb.append("0");
            sb.append(tmp);

        }
        return (new String(sb));
    }

發佈了38 篇原創文章 · 獲贊 7 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章