Java Unicode to Chinese unicodeToGB2312StrWeb(String str)

Here you can find the source of unicodeToGB2312StrWeb(String str)

Description

unicode To GB Str Web

License

Apache License

Declaration

public static String unicodeToGB2312StrWeb(String str) 

Method Source Code

//package com.java2s;
/*//  w w w.  j a  v  a  2s . co m
 * BJAF - Beetle J2EE Application Framework
 * ???J2EE???????????
 * ??????2003-2015 ??? (www.beetlesoft.net)
 * 
 * ??????????????????
 *<http://www.apache.org/licenses/LICENSE-2.0>
 *????????????????????????
 *
 * ???????????????????????????????
 * ??? <yuhaodong@gmail.com/>.
 */

public class Main {
    private static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };

    public static String unicodeToGB2312StrWeb(String str) {
        String s = null;
        if (str != null) {
            try {
                String s1 = new String(str.getBytes("GB2312"));
                s = convertHexStr(s1);
            } catch (Exception e) {
                s = null;
            }
        }
        return s;
    }

    public static String convertHexStr(String inStr) {
        StringBuffer sb = new StringBuffer();
        char[] myBuffer = inStr.toCharArray();
        for (int i = 0; i < inStr.length(); i++) {
            short s = (short) myBuffer[i];
            String hexS = Integer.toHexString(s).toUpperCase();
            if (hexS.length() == 8) {
                sb.append("&#x" + hexS.substring(4) + ";");
            } else {
                sb.append("&#x" + hexS + ";");
            }
        }
        return sb.toString();
    }

    /**
     * Returns a hex string representation of a 8 bit integer. Very fast.
     * Returned hex values are in uppercase.
     * 
     * @param b
     *            byte to convert to hex string
     * 
     * @return hex value
     */
    public static String toHexString(int b) {
        char[] digits = new char[2];
        b = b & 255;

        digits[0] = hexDigits[b / 0x10];
        digits[1] = hexDigits[b % 0x10];

        return new String(digits);
    }
}

Related

  1. UnicodeTochinese(String dataStr)
  2. unicodeToChinese(String str)
  3. unicodeToChinese(String unicode)