1. 程式人生 > >java 字串轉16進位制(包含漢字)

java 字串轉16進位制(包含漢字)

public static String toChineseHex(String s)
{
    String ss = s;
    byte[] bt = new byte[0];

    try {
        bt = ss.getBytes("GBK");
    }catch (Exception e){
        e.printStackTrace();
    }
    String s1 = "";
    for (int i = 0; i < bt.length; i++)
    {
        String tempStr = Integer.toHexString
(bt[i]); if (tempStr.length() > 2) tempStr = tempStr.substring(tempStr.length() - 2); s1 = s1 + tempStr + ""; } return s1.toUpperCase(); }