Java 机内码 String 转化

前言

新项目要跟硬件那边通信,传输的字符串类型中包含汉字,硬件那边要求转成机内码,并且反传的数据中,汉字也是转化成机内码的,这就要求我这边机内码和String能相互转化。但是但是但是,机内码是个鬼啊,我也不知道啊,我们硬件给我出了注意,把汉字和机内码的对应写成常量,你自己去查,我心里万马奔腾,还能这么搞,你不是搞笑吗。然后我就去goole了,好在万能的前辈实现了汉字转机内码,我只需要把这个逻辑反过来就能实现机内码转汉字,哈哈哈,果然是聪明无比。

汉字转机内码

至于用到的区位码,机内码是什么,大家可以自行去百度,我就不普及了,我这里只列出怎么实现

    /**
     * 汉字转化成区位码
     *
     * @param str
     * @return
     * @throws Exception
     */
    private static int[] getQuweiMa(String str) {
        byte[] b = new byte[0];
        try {
            b = str.getBytes("gb2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        int[] quwei = new int[b.length / 2];
        for (int i = 0, k = b.length / 2; i < k; i++) {
            quwei[i] = (((b[2 * i] - 0xA0) & 0xff) * 100) + ((b[2 * i + 1] - 0xA0) & 0xff);
        }
        return quwei;
    }
    
    
    /**
     * 转换成机内码
     *
     * @param str
     * @return
     * @throws Exception
     */
    public static String string2JiNeiMa(String str) {
        int[] quwei = getQuweiMa(str);
        String s = "";
        for (int i : quwei) {
            s += i + ",";
        }

        String[] stringArray = s.split(",");

        StringBuffer jineima = new StringBuffer();
        for (String ss : stringArray) {

            String hex1 = Integer.toHexString(Integer.parseInt(ss.substring(0, 2)));
            String NewHex1 = hex1.length() > 1 ? hex1 : "0" + hex1;

            String hex2 = Integer.toHexString(Integer.parseInt(ss.substring(2, 4)));
            String NewHex2 = hex2.length() > 1 ? hex2 : "0" + hex2;

            long x1 = Long.parseLong(NewHex1, 16);
            long x2 = Long.parseLong(NewHex2, 16);
            long y = Long.parseLong("A0", 16);

            jineima.append((Long.toHexString(x1 + y).toUpperCase()) + (Long.toHexString(x2 + y).toUpperCase())).append(",");
        }
        return jineima.substring(0, jineima.length() - 1).toString();
    }

机内码转汉字

    /**
     * 机内码转汉字
     * @param jiNeiMas
     * @return
     */
    public static String jiNeiMaToString(String jiNeiMas) {
        String[] jineimaArray = jiNeiMas.toLowerCase().split(",");
        StringBuffer quweiMaArray = new StringBuffer();
        for (String jiNeiMa : jineimaArray) {
            String low = jiNeiMa.substring(0, 2);
            String high = jiNeiMa.substring(2);
            long z1 = Long.parseLong(low, 16);
            long z2 = Long.parseLong(high, 16);
            long y = Long.parseLong("A0", 16);
            long x1 = z1 - y;
            long x2 = z2 - y;
            String newHex1 = Long.toHexString(x1);
            String newHex2 = Long.toHexString(x2);
            String hex1 = Integer.parseInt(newHex1, 16) + "";
            hex1 = hex1.length() > 1 ? hex1 : "0" + hex1;
            String hex2 = Integer.parseInt(newHex2, 16) + "";
            hex2 = hex2.length() > 1 ? hex2 : "0" + hex2;
            String quWeiMa = hex1 + hex2;
            quweiMaArray.append(quWeiMa);
        }
        String result = quweiToChinese(quweiMaArray.toString());
        return result;
    }
    
    
    
     /**
     * 区位码转汉字
     * @param code
     * @return
     */
    private static String quweiToChinese(String code) {
        StringBuffer chinese = new StringBuffer();
        for (int i = 0; i < code.length(); i += 4) {
            byte[] bytes = new byte[2];
            String lowCode = code.substring(i, i + 2);
            int tempLow = Integer.parseInt(lowCode);
            tempLow += 160;
            bytes[0] = (byte) tempLow;

            String highCode = code.substring(i + 2, i + 4);
            int tempHigh = Integer.parseInt(highCode);
            tempHigh += 160;
            bytes[1] = (byte) tempHigh;
            String chara = null;
            try {
                chara = new String(bytes, "gb2312");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            chinese.append(chara);
        }
        return chinese.toString();
    }
    
    
    /**
     * 判断是否为汉字
     * @param string
     * @return
     */
    public static boolean isChinese(String string) {
        int n = 0;
        for (int i = 0; i < string.length(); i++) {
            n = (int) string.charAt(i);
            if (!(19968 <= n && n < 40869)) {
                return false;
            }
        }
        return true;
    }

测试

    public static void main(String[] args) throws Exception {

        String hanzi = "京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川黔滇藏陕甘青宁新台港澳随随便便";
        System.out.println("汉字原文:" + hanzi);
        String jiNeiMa = string2JiNeiMa(hanzi);
        System.out.println("汉字转机内码:" + jiNeiMa);
        System.out.println("机内码转汉字:" + jiNeiMaToString(jiNeiMa));

    }

Java 机内码 String 转化_第1张图片

结束

我拿着机内码表对比了一下,完美。

你可能感兴趣的:(java)