无符号byte数据操作,多个无符号byte合并,byte解析成多个二进制数据

来源:互联网 发布:淘宝网羽绒棉裤女 编辑:程序博客网 时间:2024/06/16 01:42

获取到流中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;
    }
}



0 0
原创粉丝点击