無符號byte數據操作,多個無符號byte合併,byte解析成多個二進制數據

獲取到流中byte數據後,對byte進行需要的各種操作。

查了網上許多方式,實現了對byte的一些操作:

byte數據操作:

public class DataTypeConvert {

    /**
     * byte 的每一bit 轉換成byte數組中的一個值
     *
     * @param b
     * @return
     */
    public static byte[] getBooleanArray(byte b) {
        byte[] array = new byte[8];
        for (int i = 7; i >= 0; i--) {
            array[7 - i] = (byte) (b & 1);
            b = (byte) (b >> 1);
        }
        return array;
    }

    /**
     * byte數組中的 每個byte中的每個bit 解析到集合中。返回集合
     *
     * @param b
     * @return
     */
    public static List<Byte> getBooleanList(byte[] bs) {
        List<Byte> ls = new ArrayList<Byte>();
        for (int i = 0; i < bs.length; i++) {
            byte b = bs[i];
            byte[] booleanArray = getBooleanArray(b);
            for (int j = 0; j < booleanArray.length; j++) {
                ls.add(booleanArray[j]);
            }
        }
        // byte[] array = new byte[8];
        // for (int i = 7; i >= 0; i--) {
        // array[i] = (byte) (b & 1);
        // b = (byte) (b >> 1);
        // }
        return ls;
    }

    /**
     * 無符號Int轉long
     *
     * @param num
     * @return
     */
    public static long intToLong(int num) {
        return (long) num & 0x00ffffffffL;
    }

    /**
     * 四個無符號byte合併爲無符號 int
     *
     * @param b1
     * @param b2
     * @param b3
     * @param b4
     * @return
     */
    public static int fourByteToInt(byte b1, byte b2, byte b3, byte b4) {
        return (int) (((b1 & 0x000000ff) << 24) | ((b2 & 0x000000ff) << 16)
                | ((b3 & 0x000000ff) << 8) | (b4 & 0x000000ff));
    }

    /**
     * 分割byte數組,傳遞byte數組,然後傳遞三位長度
     *
     * @param bs
     * @param a1Len
     * @param a2Len
     * @param a3Len
     * @return
     */
    public static List<byte[]> subBytes(byte[] bs, int a1Len, int a2Len,
            int a3Len) {

        byte[] byte1 = new byte[a1Len];
        byte[] byte2 = new byte[a2Len];
        byte[] byte3 = new byte[a3Len];
        List<byte[]> arrayBytes = new ArrayList<>();
        for (int i = 0; i < a1Len; i++) {
            byte1[i] = bs[i];
        }
        for (int i = 0; i < a2Len; i++) {
            byte2[i] = bs[i + a1Len];
        }
        for (int i = 0; i < a3Len; i++) {
            byte3[i] = bs[i + a1Len + a2Len];
        }
        arrayBytes.add(byte1);
        arrayBytes.add(byte2);
        arrayBytes.add(byte3);
        bs = null;
        return arrayBytes;

    }

    /**
     * 兩個無符號byte合併爲無符號short
     *
     * @param hi
     * @param lo
     * @return
     */
    public static short twoByteToShot(byte hi, byte lo) {
        return (short) (((hi & 0x00FF) << 8) | (0x00FF & lo));
    }

    /**
     * 無符號short轉int
     *
     * @param sh
     * @return
     */
    public static int shotToInt(short sh) {
        return sh & 0xffff;
    }

    /**
     * int 轉換成 無符號byte
     *
     * @param num
     * @return
     */
    public static byte[] intToBytes(int num) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) (num >>> (24 - i * 8));
        }
        return b;
    }

    /**
     * byte 轉成無符號Int
     *
     * @param bb
     * @return
     */
    public static int byteToInt(byte bb) {
        return bb & 0xff;
    }
}



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