java socket解析和發送二進制報文工具(附java和C++轉化問題)


解析:

首先是讀取字節:

/**
     * 讀取輸入流中指定字節的長度
     * <p/>
     * 輸入流
     *
     * @param length 指定長度
     * @return 指定長度的字節數組
     */
    public static byte[] readBytesFromTo(byte[] buffer, int from, int length) {
        byte[] sub = new byte[length];
        int cur = 0;
        for (int i = from; i < length + from; i++) {
            sub[cur] = buffer[i];
            cur++;
        }
        return sub;
    }

讀取之后轉為字符串或者整型:

    /**
     * byte[]轉int
     *
     * @param bytes 報文
     * @return Integer
     */
    public static int byteArrayToInt(byte[] bytes) {
        int value = 0;
        // 由高位到低位
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;// 往高位游
        }
        return value;
    }
  /**
     * 字節轉字符串
     * @param bytes 報文
     */
    public  static String byteArrayToString(byte[] bytes,int start , int end){
        return new String(bytes, start, end);
    }

發送報文:

將java類型轉化為二進制:

   /**
     * @param i
     * @return
     */
    public static byte[] intToByteArray(int i) {
        byte[] result = new byte[4];
        result[0] = (byte) ((i >> 24) & 0xFF);
        result[1] = (byte) ((i >> 16) & 0xFF);
        result[2] = (byte) ((i >> 8) & 0xFF);
        result[3] = (byte) (i & 0xFF);
        return result;
    }

    /**
     * 
     * @param s
     * @return
     */
    public static byte[] StringToByteArray(String s) {
        return s.getBytes();
    }

整合二進制數組:

    /**
     * 
     * @param bytes
     * @return
     */
    public    static byte[]  combineByte(byte[] ... bytes){
        int length = 0;
        for (byte[] b : bytes) {
            length+=b.length;
        }
        byte[] allByte=new byte[length];
        int positon=0;
        for (byte[] b : bytes) {
            System.arraycopy(b, 0, allByte, positon, b.length);
            positon+=b.length;
        }
        return allByte;
    }

求校驗和:

  /**
     *
     * @param input
     * @return
     */
    public static int getCheckSum(byte[]... input) {
        int re = 0;
        for (byte[] b : input) {
            for (byte aB : b) {
                re += aB & 0x0FF;//注意此處要轉化為無符號
            }
        }
        return re;
    }

二進制內容有時候要在不同的環境下解析和發送,下面是C++和java的字符差異

下面給出在不同字符集編碼下的字節數:

          英文字母:

                  字節數 : 1;編碼:GB2312             字節數: 1;編碼:GBK            字節數 : 1;編碼:GB18030

                  字節數 : 1;編碼:ISO-8859-1        字節數: 1;編碼:UTF-8         字節數 : 4;編碼:UTF-16

                  字節數 : 2;編碼:UTF-16BE           字節數: 2;編碼:UTF-16LE

         中文漢字:

                  字節數 : 2;編碼:GB2312              字節數: 2;編碼:GBK            字節數 : 2;編碼:GB18030

                  字節數 : 1;編碼:ISO-8859-1        字節數: 3;編碼:UTF-8         字節數 : 4;編碼:UTF-16

                  字節數 : 2;編碼:UTF-16BE           字節數: 2;編碼:UTF-16LE

編碼不同會影響解析的方式的差異,有時候還是蠻頭疼的,比如我們常用的中文問題,C++默認用GB2312編碼,所以java的解析要變一下:

String describe = new String(bytes, start += 4, describeLength, Charset.forName("GB2312"));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM