java 16進制和int 轉換

package com.android.utils;

import java.nio.charset.Charset;

import org.apache.commons.lang3.ArrayUtils;

/**
 * 字節數組轉換工具類
 */
public class BytesUtils {

    public static final String GBK = "GBK";
    public static final String UTF8 = "utf-8";
    public static final char[] ascii = "0123456789ABCDEF".toCharArray();
    private static char[] HEX_VOCABLE = {'0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    /**
     * 將short整型數值轉換爲字節數組
     * 
     * @param data
     * @return
     */
    public static byte[] getBytes(short data) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) ((data & 0xff00) >> 8);
        bytes[1] = (byte) (data & 0xff);
        return bytes;
    }

    /**
     * 將字符轉換爲字節數組
     * 
     * @param data
     * @return
     */
    public static byte[] getBytes(char data) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (data >> 8);
        bytes[1] = (byte) (data);
        return bytes;
    }

    /**
     * 將布爾值轉換爲字節數組
     * 
     * @param data
     * @return
     */
    public static byte[] getBytes(boolean data) {
        byte[] bytes = new byte[1];
        bytes[0] = (byte) (data ? 1 : 0);
        return bytes;
    }

    /**
     * 將整型數值轉換爲字節數組
     * 
     * @param data
     * @return
     */
    public static byte[] getBytes(int data) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) ((data & 0xff000000) >> 24);
        bytes[1] = (byte) ((data & 0xff0000) >> 16);
        bytes[2] = (byte) ((data & 0xff00) >> 8);
        bytes[3] = (byte) (data & 0xff);
        return bytes;
    }

    /**
     * 將long整型數值轉換爲字節數組
     * 
     * @param data
     * @return
     */
    public static byte[] getBytes(long data) {
        byte[] bytes = new byte[8];
        bytes[0] = (byte) ((data >> 56) & 0xff);
        bytes[1] = (byte) ((data >> 48) & 0xff);
        bytes[2] = (byte) ((data >> 40) & 0xff);
        bytes[3] = (byte) ((data >> 32) & 0xff);
        bytes[4] = (byte) ((data >> 24) & 0xff);
        bytes[5] = (byte) ((data >> 16) & 0xff);
        bytes[6] = (byte) ((data >> 8) & 0xff);
        bytes[7] = (byte) (data & 0xff);
        return bytes;
    }

    /**
     * 將float型數值轉換爲字節數組
     * 
     * @param data
     * @return
     */
    public static byte[] getBytes(float data) {
        int intBits = Float.floatToIntBits(data);
        return getBytes(intBits);
    }

    /**
     * 將double型數值轉換爲字節數組
     * 
     * @param data
     * @return
     */
    public static byte[] getBytes(double data) {
        long intBits = Double.doubleToLongBits(data);
        return getBytes(intBits);
    }

    /**
     * 將字符串按照charsetName編碼格式的字節數組
     * 
     * @param data
     *            字符串
     * @param charsetName
     *            編碼格式
     * @return
     */
    public static byte[] getBytes(String data, String charsetName) {
        Charset charset = Charset.forName(charsetName);
        return data.getBytes(charset);
    }

    /**
     * 將字符串按照GBK編碼格式的字節數組
     * 
     * @param data
     * @return
     */
    public static byte[] getBytes(String data) {
        return getBytes(data, GBK);
    }

    /**
     * 將字節數組第0字節轉換爲布爾值
     * 
     * @param bytes
     * @return
     */
    public static boolean getBoolean(byte[] bytes) {
        return bytes[0] == 1;
    }

    /**
     * 將字節數組的第index字節轉換爲布爾值
     * 
     * @param bytes
     * @param index
     * @return
     */
    public static boolean getBoolean(byte[] bytes, int index) {
        return bytes[index] == 1;
    }

    /**
     * 將字節數組前2字節轉換爲short整型數值
     * 
     * @param bytes
     * @return
     */
    public static short getShort(byte[] bytes) {
        return (short) ((0xff00 & (bytes[0] << 8)) | (0xff & bytes[1]));
    }

    /**
     * 將字節數組從startIndex開始的2個字節轉換爲short整型數值
     * 
     * @param bytes
     * @param startIndex
     * @return
     */
    public static short getShort(byte[] bytes, int startIndex) {
        return (short) ((0xff00 & (bytes[startIndex] << 8))
                | (0xff & bytes[startIndex + 1]));
    }

    /**
     * 將字節數組前2字節轉換爲字符
     * 
     * @param bytes
     * @return
     */
    public static char getChar(byte[] bytes) {
        return (char) ((0xff00 & (bytes[0] << 8)) | (0xff & bytes[1]));
    }

    /**
     * 將字節數組從startIndex開始的2個字節轉換爲字符
     * 
     * @param bytes
     * @param startIndex
     * @return
     */
    public static char getChar(byte[] bytes, int startIndex) {
        return (char) ((0xff00 & (bytes[startIndex] << 8))
                | (0xff & bytes[startIndex + 1]));
    }

    /**
     * 將字節數組前4字節轉換爲整型數值
     * 
     * @param bytes
     * @return
     */
    public static int getInt(byte[] bytes) {
        return (0xff000000 & (bytes[0] << 24) | (0xff0000 & (bytes[1] << 16))
                | (0xff00 & (bytes[2] << 8)) | (0xff & bytes[3]));
    }

    /**
     * 將字節數組從startIndex開始的4個字節轉換爲整型數值
     * 
     * @param bytes
     * @param startIndex
     * @return
     */
    public static int getInt(byte[] bytes, int startIndex) {
        return (0xff000000 & (bytes[startIndex] << 24)
                | (0xff0000 & (bytes[startIndex + 1] << 16))
                | (0xff00 & (bytes[startIndex + 2] << 8))
                | (0xff & bytes[startIndex + 3]));
    }

    /**
     * 將字節數組前8字節轉換爲long整型數值
     * 
     * @param bytes
     * @return
     */
    public static long getLong(byte[] bytes) {
        return (0xff00000000000000L & ((long) bytes[0] << 56)
                | (0xff000000000000L & ((long) bytes[1] << 48))
                | (0xff0000000000L & ((long) bytes[2] << 40))
                | (0xff00000000L & ((long) bytes[3] << 32))
                | (0xff000000L & ((long) bytes[4] << 24))
                | (0xff0000L & ((long) bytes[5] << 16))
                | (0xff00L & ((long) bytes[6] << 8))
                | (0xffL & (long) bytes[7]));
    }

    /**
     * 將字節數組從startIndex開始的8個字節轉換爲long整型數值
     * 
     * @param bytes
     * @param startIndex
     * @return
     */
    public static long getLong(byte[] bytes, int startIndex) {
        return (0xff00000000000000L & ((long) bytes[startIndex] << 56)
                | (0xff000000000000L & ((long) bytes[startIndex + 1] << 48))
                | (0xff0000000000L & ((long) bytes[startIndex + 2] << 40))
                | (0xff00000000L & ((long) bytes[startIndex + 3] << 32))
                | (0xff000000L & ((long) bytes[startIndex + 4] << 24))
                | (0xff0000L & ((long) bytes[startIndex + 5] << 16))
                | (0xff00L & ((long) bytes[startIndex + 6] << 8))
                | (0xffL & (long) bytes[startIndex + 7]));
    }

    /**
     * 將字節數組前4字節轉換爲float型數值
     * 
     * @param bytes
     * @return
     */
    public static float getFloat(byte[] bytes) {
        return Float.intBitsToFloat(getInt(bytes));
    }

    /**
     * 將字節數組從startIndex開始的4個字節轉換爲float型數值
     * 
     * @param bytes
     * @param startIndex
     * @return
     */
    public static float getFloat(byte[] bytes, int startIndex) {
        byte[] result = new byte[4];
        System.arraycopy(bytes, startIndex, result, 0, 4);
        return Float.intBitsToFloat(getInt(result));
    }

    /**
     * 將字節數組前8字節轉換爲double型數值
     * 
     * @param bytes
     * @return
     */
    public static double getDouble(byte[] bytes) {
        long l = getLong(bytes);
        return Double.longBitsToDouble(l);
    }

    /**
     * 將字節數組從startIndex開始的8個字節轉換爲double型數值
     * 
     * @param bytes
     * @param startIndex
     * @return
     */
    public static double getDouble(byte[] bytes, int startIndex) {
        byte[] result = new byte[8];
        System.arraycopy(bytes, startIndex, result, 0, 8);
        long l = getLong(result);
        return Double.longBitsToDouble(l);
    }

    /**
     * 將charsetName編碼格式的字節數組轉換爲字符串
     * 
     * @param bytes
     * @param charsetName
     * @return
     */
    public static String getString(byte[] bytes, String charsetName) {
        return new String(bytes, Charset.forName(charsetName));
    }

    /**
     * 將GBK編碼格式的字節數組轉換爲字符串
     * 
     * @param bytes
     * @return
     */
    public static String getString(byte[] bytes) {
        return getString(bytes, GBK);
    }

    /**
     * 將16進制字符串轉換爲字節數組
     * 
     * @param hex
     * @return
     */
    // @Deprecated
    // public static byte[] hexStringToBytes(String hex) {
    // if (hex == null || "".equals(hex)) {
    // return null;
    // }
    // int len = hex.length() / 2;
    // byte[] result = new byte[len];
    // char[] chArr = hex.toCharArray();
    // for (int i = 0; i < len; i++) {
    // int pos = i * 2;
    // result[i] = (byte) (toByte(chArr[pos]) << 4 | toByte(chArr[pos + 1]));
    // }
    // return result;
    // }

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        // toUpperCase將字符串中的所有字符轉換爲大寫
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        // toCharArray將此字符串轉換爲一個新的字符數組。
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4
                    | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    // charToByte返回在指定字符的第一個發生的字符串中的索引,即返回匹配字符
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
    /**
     * 將16進制字符串轉換爲字節數組
     * 
     * @param hex
     * @return
     */
    public static byte[] hexToBytes(String hex) {
        if (hex.length() % 2 != 0)
            throw new IllegalArgumentException(
                    "input string should be any multiple of 2!");
        hex.toUpperCase();

        byte[] byteBuffer = new byte[hex.length() / 2];

        byte padding = 0x00;
        boolean paddingTurning = false;
        for (int i = 0; i < hex.length(); i++) {
            if (paddingTurning) {
                char c = hex.charAt(i);
                int index = indexOf(hex, c);
                padding = (byte) ((padding << 4) | index);
                byteBuffer[i / 2] = padding;
                padding = 0x00;
                paddingTurning = false;
            } else {
                char c = hex.charAt(i);
                int index = indexOf(hex, c);
                padding = (byte) (padding | index);
                paddingTurning = true;
            }

        }
        return byteBuffer;
    }

    private static int indexOf(String input, char c) {
        int index = ArrayUtils.indexOf(HEX_VOCABLE, c);

        if (index < 0) {
            throw new IllegalArgumentException("err input:" + input);
        }
        return index;

    }

    /**
     * 將BCD編碼的字節數組轉換爲字符串
     * 
     * @param bcds
     * @return
     */
    public static String bcdToString(byte[] bcds) {
        if (bcds == null || bcds.length == 0) {
            return null;
        }
        byte[] temp = new byte[2 * bcds.length];
        for (int i = 0; i < bcds.length; i++) {
            temp[i * 2] = (byte) ((bcds[i] >> 4) & 0x0f);
            temp[i * 2 + 1] = (byte) (bcds[i] & 0x0f);
        }
        StringBuffer res = new StringBuffer();
        for (int i = 0; i < temp.length; i++) {
            res.append(ascii[temp[i]]);
        }
        return res.toString();
    }

    /**
     * 字節轉整形
     * 
     * @param value
     * @return
     */
    public static int bcdToInt(byte value) {
        return ((value >> 4) * 10) + (value & 0x0F);
    }

    /**
     * 字節數組轉16進制字符串
     * 
     * @param bs
     * @return
     */
    public static String bytesToHex(byte[] bs) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bs) {
            int high = (b >> 4) & 0x0f;
            int low = b & 0x0f;
            sb.append(HEX_VOCABLE[high]);
            sb.append(HEX_VOCABLE[low]);
        }
        return sb.toString();
    }

    /**
     * 字節數組取前len個字節轉16進制字符串
     * 
     * @param bs
     * @param len
     * @return
     */
    public static String bytesToHex(byte[] bs, int len) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            byte b = bs[i];
            int high = (b >> 4) & 0x0f;
            int low = b & 0x0f;
            sb.append(HEX_VOCABLE[high]);
            sb.append(HEX_VOCABLE[low]);
        }
        return sb.toString();
    }
    /**
     * 字節數組偏移offset長度之後的取len個字節轉16進制字符串
     * 
     * @param bs
     * @param offset
     * @param len
     * @return
     */
    public static String bytesToHex(byte[] bs, int offset, int len) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            byte b = bs[offset + i];
            int high = (b >> 4) & 0x0f;
            int low = b & 0x0f;
            sb.append(HEX_VOCABLE[high]);
            sb.append(HEX_VOCABLE[low]);
        }
        return sb.toString();
    }
    /**
     * 字節數組轉16進制字符串
     * 
     * @param bs
     * @return
     */
    public static String byteToHex(byte b) {
        StringBuilder sb = new StringBuilder();
        int high = (b >> 4) & 0x0f;
        int low = b & 0x0f;
        sb.append(HEX_VOCABLE[high]);
        sb.append(HEX_VOCABLE[low]);
        return sb.toString();
    }
    /**
     * 將字節數組取反
     * 
     * @param src
     * @return
     */
    public static String negate(byte[] src) {
        if (src == null || src.length == 0) {
            return null;
        }
        byte[] temp = new byte[2 * src.length];
        for (int i = 0; i < src.length; i++) {
            byte tmp = (byte) (0xFF ^ src[i]);
            temp[i * 2] = (byte) ((tmp >> 4) & 0x0f);
            temp[i * 2 + 1] = (byte) (tmp & 0x0f);
        }
        StringBuffer res = new StringBuffer();
        for (int i = 0; i < temp.length; i++) {
            res.append(ascii[temp[i]]);
        }
        return res.toString();
    }

    /**
     * 比較字節數組是否相同
     * 
     * @param a
     * @param b
     * @return
     */
    public static boolean compareBytes(byte[] a, byte[] b) {
        if (a == null || a.length == 0 || b == null || b.length == 0
                || a.length != b.length) {
            return false;
        }
        if (a.length == b.length) {
            for (int i = 0; i < a.length; i++) {
                if (a[i] != b[i]) {
                    return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }
    /**
     * 只比對指定長度byte
     * 
     * @param a
     * @param b
     * @param len
     * @return
     */
    public static boolean compareBytes(byte[] a, byte[] b, int len) {
        if (a == null || a.length == 0 || b == null || b.length == 0
                || a.length < len || b.length < len) {
            return false;
        }
        for (int i = 0; i < len; i++) {
            if (a[i] != b[i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 將字節數組轉換爲二進制字符串
     * 
     * @param items
     * @return
     */
    public static String bytesToBinaryString(byte[] items) {
        if (items == null || items.length == 0) {
            return null;
        }
        StringBuffer buf = new StringBuffer();
        for (byte item : items) {
            buf.append(byteToBinaryString(item));
        }
        return buf.toString();
    }

    /**
     * 將字節轉換爲二進制字符串
     * 
     * @param items
     * @return
     */
    public static String byteToBinaryString(byte item) {
        byte a = item;
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < 8; i++) {
            buf.insert(0, a % 2);
            a = (byte) (a >> 1);
        }
        return buf.toString();
    }

    /**
     * 對數組a,b進行異或運算
     * 
     * @param a
     * @param b
     * @return
     */
    public static byte[] xor(byte[] a, byte[] b) {
        if (a == null || a.length == 0 || b == null || b.length == 0
                || a.length != b.length) {
            return null;
        }
        byte[] result = new byte[a.length];
        for (int i = 0; i < a.length; i++) {
            result[i] = (byte) (a[i] ^ b[i]);
        }
        return result;
    }

    /**
     * 對數組a,b進行異或運算 運算長度len
     * 
     * @param a
     * @param b
     * @param len
     * @return
     */
    public static byte[] xor(byte[] a, byte[] b, int len) {
        if (a == null || a.length == 0 || b == null || b.length == 0) {
            return null;
        }
        if (a.length < len || b.length < len) {
            return null;
        }
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++) {
            result[i] = (byte) (a[i] ^ b[i]);
        }
        return result;
    }
    /**
     * 將short整型數值轉換爲字節數組
     * 
     * @param num
     * @return
     */
    public static byte[] shortToBytes(int num) {
        byte[] temp = new byte[2];
        for (int i = 0; i < 2; i++) {
            temp[i] = (byte) ((num >>> (8 - i * 8)) & 0xFF);
        }
        return temp;
    }

    /**
     * 將字節數組轉爲整型
     * 
     * @param num
     * @return
     */
    public static int bytesToShort(byte[] arr) {
        int mask = 0xFF;
        int temp = 0;
        int result = 0;
        for (int i = 0; i < 2; i++) {
            result <<= 8;
            temp = arr[i] & mask;
            result |= temp;
        }
        return result;
    }

    /**
     * 將整型數值轉換爲指定長度的字節數組
     * 
     * @param num
     * @return
     */
    public static byte[] intToBytes(int num) {
        byte[] temp = new byte[4];
        for (int i = 0; i < 4; i++) {
            temp[i] = (byte) ((num >>> (24 - i * 8)) & 0xFF);
        }
        return temp;
    }

    /**
     * 將整型數值轉換爲指定長度的字節數組
     * 
     * @param src
     * @param len
     * @return
     */
    public static byte[] intToBytes(int src, int len) {
        if (len < 1 || len > 4) {
            return null;
        }
        byte[] temp = new byte[len];
        for (int i = 0; i < len; i++) {
            temp[len - 1 - i] = (byte) ((src >>> (8 * i)) & 0xFF);
        }
        return temp;
    }

    /**
     * 將字節數組轉換爲整型數值
     * 
     * @param arr
     * @return
     */
    public static int bytesToInt(byte[] arr) {
        int mask = 0xFF;
        int temp = 0;
        int result = 0;
        for (int i = 0; i < 4; i++) {
            result <<= 8;
            temp = arr[i] & mask;
            result |= temp;
        }
        return result;
    }

    /**
     * 將long整型數值轉換爲字節數組
     * 
     * @param num
     * @return
     */
    public static byte[] longToBytes(long num) {
        byte[] temp = new byte[8];
        for (int i = 0; i < 8; i++) {
            temp[i] = (byte) ((num >>> (56 - i * 8)) & 0xFF);
        }
        return temp;
    }

    /**
     * 將字節數組轉換爲long整型數值
     * 
     * @param arr
     * @return
     */
    public static long bytesToLong(byte[] arr) {
        int mask = 0xFF;
        int temp = 0;
        long result = 0;
        int len = Math.min(8, arr.length);
        for (int i = 0; i < len; i++) {
            result <<= 8;
            temp = arr[i] & mask;
            result |= temp;
        }
        return result;
    }

    /**
     * 將16進制字符轉換爲字節
     * 
     * @param c
     * @return
     */
    public static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }

    /**
     * 功能描述:把兩個字節的字節數組轉化爲整型數據,高位補零,例如:<br/>
     * 有字節數組byte[] data = new byte[]{1,2};轉換後int數據的字節分佈如下:<br/>
     * 00000000 00000000 00000001 00000010,函數返回258
     * 
     * @param lenData
     *            需要進行轉換的字節數組
     * @return 字節數組所表示整型值的大小
     */
    public static int bytesToIntWhereByteLengthEquals2(byte lenData[]) {
        if (lenData.length != 2) {
            return -1;
        }
        byte fill[] = new byte[]{0, 0};
        byte real[] = new byte[4];
        System.arraycopy(fill, 0, real, 0, 2);
        System.arraycopy(lenData, 0, real, 2, 2);
        int len = byteToInt(real);
        return len;

    }

    /**
     * 功能描述:將byte數組轉化爲int類型的數據
     * 
     * @param byteVal
     *            需要轉化的字節數組
     * @return 字節數組所表示的整型數據
     */
    public static int byteToInt(byte[] byteVal) {
        int result = 0;
        for (int i = 0; i < byteVal.length; i++) {
            int tmpVal = (byteVal[i] << (8 * (3 - i)));
            switch (i) {
                case 0 :
                    tmpVal = tmpVal & 0xFF000000;
                    break;
                case 1 :
                    tmpVal = tmpVal & 0x00FF0000;
                    break;
                case 2 :
                    tmpVal = tmpVal & 0x0000FF00;
                    break;
                case 3 :
                    tmpVal = tmpVal & 0x000000FF;
                    break;
            }

            result = result | tmpVal;
        }
        return result;
    }
    public static byte CheckXORSum(byte[] bData) {
        byte sum = 0x00;
        for (int i = 0; i < bData.length; i++) {
            sum ^= bData[i];
        }
        return sum;
    }
    /**
     * 從offset開始 將後續長度爲len的byte字節轉爲int
     * 
     * @param data
     * @param offset
     * @param len
     * @return
     */
    public static int bytesToInt(byte[] data, int offset, int len) {
        int mask = 0xFF;
        int temp = 0;
        int result = 0;
        len = Math.min(len, 4);
        for (int i = 0; i < len; i++) {
            result <<= 8;
            temp = data[offset + i] & mask;
            result |= temp;
        }
        return result;
    }

    /**
     * byte字節數組中的字符串的長度
     * 
     * @param data
     * @return
     */
    public static int getBytesStringLen(byte[] data) {
        int count = 0;
        for (byte b : data) {
            if (b == 0x00)
                break;
            count++;
        }
        return count;
    }

    /**
     * 校驗和
     * 
     * @param msg
     *            需要計算校驗和的byte數組
     * @param length
     *            校驗和位數
     * @return 計算出的校驗和數組
     */
    public static byte[] SumCheck(byte[] msg, int length) {
        long mSum = 0;
        byte[] mByte = new byte[length];

        /** 逐Byte添加位數和 */
        for (byte byteMsg : msg) {
            long mNum = ((long) byteMsg >= 0)
                    ? (long) byteMsg
                    : ((long) byteMsg + 256);
            mSum += mNum;
        } /** end of for (byte byteMsg : msg) */

        /** 位數和轉化爲Byte數組 */
        for (int liv_Count = 0; liv_Count < length; liv_Count++) {
            mByte[length - liv_Count
                    - 1] = (byte) (mSum >> (liv_Count * 8) & 0xff);
        } /** end of for (int liv_Count = 0; liv_Count < length; liv_Count++) */

        return mByte;
    }

    /**
     * 字節數據轉十進制字符(補碼-無符號)
     * 
     * @param data
     * @return
     */
    public static String getByteToStr(byte[] data) {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            if (data[i] < 0) {
                int tem = data[i] + 256;
                str.append(tem + " ");
            } else {
                str.append(data[i] + " ");
            }
        }
        return str.toString();
    }

}

 

應用


  
    /**  
     *  【以太網數據協議】根據發送的數據來封裝十六進制字符串格式數據   數據長度 兩個字節
     * 
     * @param responseData
     * @param resCommand
     * @return
     */
    private static String getResponseCommand(String responseData, String resCommand) {
        String len=BytesUtils.bytesToHex(BytesUtils.intToBytes((responseData.length()/2+9),2));
        //頭部+長度+加密+機器設備號+協議命令類型+數據+校驗和
        String result="AE"+len+"00"+"080808080808"+resCommand+responseData;
        String xiaoyanhe=result+"59";
        //計算校驗和  (一個字節)
        String sum=BytesUtils.bytesToHex(BytesUtils.SumCheck(BytesUtils.hexStringToBytes(xiaoyanhe), 1));
        System.out.println("校驗和:"+sum);
        result=result+sum;
        return result;
    }
    
    
    
    /**
     * 【串口數據協議】根據發送的數據來封裝十六進制字符串格式數據   數據長度 兩個字節
     * 
     * @param responseData  協議數據部分
     * @param resCommand    協議命令類型
     * @return
     */
    private static String getResponseCommand2(String responseData, String resCommand) {
        String len=BytesUtils.bytesToHex(BytesUtils.intToBytes((responseData.length()/2+2),2));
        //頭部+長度+協議命令類型+數據+校驗和
        String result="AE"+len+resCommand+responseData;
        String xiaoyanhe=result;
        //計算校驗和  (一個字節)
        String sum=BytesUtils.bytesToHex(BytesUtils.SumCheck(BytesUtils.hexStringToBytes(xiaoyanhe), 1));
        System.out.println("校驗和:"+sum);
        result=result+sum;
        return result;
    }
    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章