两个byte数据和int型数据互转

1,360 阅读1分钟
    /**
     * 将两个byte 合并转化为一个 hex 数据
     *
     * @param high 高位数据
     * @param low  低位数据
     * @return 返回的数据 高位在前,低位在后。
     */
    public static int mergeByte2Hex(byte high, byte low) {
        return (int) ((high & 0xff) << 8 | (low & 0xff));
    }

    /**
     * 将一个 int 型数据转化为两个byte 数据
     * @param value int 数值
     * @return  两个字节的byte 数组
     */
    public static byte[] intToByteArray(int value) {
        byte[] mValue = new byte[2];
        mValue[0] = (byte) ((value >> 8) & 0xFF);
        mValue[1] = (byte) (value & 0xFF);
        return mValue;
    }