目录

代码 


代码 

//中文转unicode编码
     public static String gbEncoding(final String gbString) {
         char[] utfBytes = gbString.toCharArray();
         String unicodeBytes = "";
         for (int i = 0; i < utfBytes.length; i++) {
             String hexB = Integer.toHexString(utfBytes[i]);
             if (hexB.length() <= 2) {
                 hexB = "00" + hexB;
             }
             unicodeBytes = unicodeBytes + "\\u" + hexB;
         }
         return unicodeBytes;
     }
     //unicode编码转中文
     public static String decodeUnicode(final String dataStr) {
         int start = 0;
         int end = 0;
         final StringBuffer buffer = new StringBuffer();
         while (start > -1) {
             end = dataStr.indexOf("\\u", start + 2);
             String charStr = "";
             if (end == -1) {
                 charStr = dataStr.substring(start + 2, dataStr.length());
             } else {
                 charStr = dataStr.substring(start + 2, end);
             }
             char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
             buffer.append(new Character(letter).toString());
             start = end;
         }
         return buffer.toString();
     }
     //汉字转换成二进制字符串
     public static String strToBinStr(String str) {
         char[] chars=str.toCharArray();
         StringBuffer result = new StringBuffer();
         for(int i=0; i<chars.length; i++) {
             result.append(Integer.toBinaryString(chars[i]));
             result.append(" ");
         }
         return result.toString();
     }
     //二进制字符串转换成汉字
     public static String BinStrTostr(String binary) {
         String[] tempStr=binary.split(" ");
         char[] tempChar=new char[tempStr.length];
         for(int i=0;i<tempStr.length;i++) {
             tempChar[i]=BinstrToChar(tempStr[i]);
         }
         return String.valueOf(tempChar);
     }
     //将二进制字符串转换成int数组
     public static int[] BinstrToIntArray(String binStr) {
         char[] temp=binStr.toCharArray();
         int[] result=new int[temp.length];
         for(int i=0;i<temp.length;i++) {
             result[i]=temp[i]-48;
         }
         return result;
     }
     //将二进制转换成字符
     public static char BinstrToChar(String binStr){
         int[] temp=BinstrToIntArray(binStr);
         int sum=0;
         for(int i=0; i<temp.length;i++){
             sum +=temp[temp.length-1-i]<<i;
         }
         return (char)sum;
     }
//中文转unicode编码
    public static String gbEncoding(final String gbString) {
        char[] utfBytes = gbString.toCharArray();
        String unicodeBytes = "";
        for (int i = 0; i < utfBytes.length; i++) {
            String hexB = Integer.toHexString(utfBytes[i]);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }
        return unicodeBytes;
    }
    //unicode编码转中文
    public static String decodeUnicode(final String dataStr) {
        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while (start > -1) {
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = dataStr.substring(start + 2, dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
            buffer.append(new Character(letter).toString());
            start = end;
        }
        return buffer.toString();
    }
    //汉字转换成二进制字符串
    public static String strToBinStr(String str) {
        char[] chars=str.toCharArray();
        StringBuffer result = new StringBuffer();
        for(int i=0; i<chars.length; i++) {
            result.append(Integer.toBinaryString(chars[i]));
            result.append(" ");
        }
        return result.toString();
    }
    //二进制字符串转换成汉字
    public static String BinStrTostr(String binary) {
        String[] tempStr=binary.split(" ");
        char[] tempChar=new char[tempStr.length];
        for(int i=0;i<tempStr.length;i++) {
            tempChar[i]=BinstrToChar(tempStr[i]);
        }
        return String.valueOf(tempChar);
    }
    //将二进制字符串转换成int数组
    public static int[] BinstrToIntArray(String binStr) {
        char[] temp=binStr.toCharArray();
        int[] result=new int[temp.length];
        for(int i=0;i<temp.length;i++) {
            result[i]=temp[i]-48;
        }
        return result;
    }
    //将二进制转换成字符
    public static char BinstrToChar(String binStr){
        int[] temp=BinstrToIntArray(binStr);
        int sum=0;
        for(int i=0; i<temp.length;i++){
            sum +=temp[temp.length-1-i]<<i;
        }
        return (char)sum;
    }