panlili2024
2024-09-19 071a8328823a2861f93ce556a4da3e4119cab1a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.hdl.sdk.ttl.Utils.HDLUtlis;
 
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
 
/**
 * Created by JLChen on 2019/7/25
 */
public class HDLUtlis {
 
    /**
     * 将object转为Integer类型
     *
     * @param object
     * @return
     */
    public static Integer getIntegerByObject(Object object) {
        Integer in = null;
        if (object != null) {
            if (object instanceof Integer) {
                in = (Integer) object;
            } else if (object instanceof String) {
                in = Integer.parseInt((String) object);
            } else if (object instanceof Double) {
                in = (int) ((double) object);
            } else if (object instanceof Float) {
                in = (int) ((float) object);
            } else if (object instanceof BigDecimal) {
                in = ((BigDecimal) object).intValue();
            } else if (object instanceof Long) {
                in = ((Long) object).intValue();
            }
        }
        return in;
    }
 
    /**
     * int类型转4字节byte数组
     *
     * @param mInt
     * @return 4字节byte数组
     */
    public static byte[] intToByteArray(int mInt) {
        byte[] result = new byte[4];
        // 由高位到低位
        result[0] = (byte) ((mInt >> 24) & 0xFF);
        result[1] = (byte) ((mInt >> 16) & 0xFF);
        result[2] = (byte) ((mInt >> 8) & 0xFF);
        result[3] = (byte) (mInt & 0xFF);
        return result;
    }
 
    /**
     * byte[]转int
     *
     * @param bytes
     * @return
     */
    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;
    }
 
 
    public static float byte2Float(byte[] bytes) {
       /* if (bytes.length != 4) {
            return 0;
        }*/
        byte b[] = bytes;
        ByteBuffer buf = ByteBuffer.allocateDirect(4);
        buf = buf.order(ByteOrder.LITTLE_ENDIAN);//小端用这行代码,默认大端转换
        buf.put(b);
        buf.rewind();
        float f2 = buf.getFloat();
        return f2;
    }
 
    /**
     * 调整int 类型参数
     *
     * @return progress
     */
    public static int getTrueProgressInt(int progress) {
        if (progress < 0) {
            progress = 0;
        } else if (progress > 100) {
            progress = 100;
        }
        return progress;
    }
 
 
    /**
     * 4 byte 转换为float类型
     *
     * @param b1
     * @param b2
     * @param b3
     * @param b4
     * @return
     */
    public static float byteToFloat(byte b1, byte b2, byte b3, byte b4) {
        byte[] mByte = new byte[4];
        mByte[0] = b1;
        mByte[1] = b2;
        mByte[2] = b3;
        mByte[3] = b4;
        return byteArrayToFloat(mByte);
    }
 
 
    /**
     * byte[4]数组 转换为float类型
     *
     * @param arr 长度为4
     * @return
     */
    public static float byteArrayToFloat(byte[] arr) {
        try {
            return Float.intBitsToFloat(getInt(arr));
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
 
 
    /**
     * 连续4个字节获得一个int
     *
     * @param arr
     * @return int
     */
    public static int getInt(byte[] arr) {
        return (0xff000000 & (arr[0] << 24)) |
                (0x00ff0000 & (arr[1] << 16)) |
                (0x0000ff00 & (arr[2] << 8)) |
                (0x000000ff & arr[3]);
    }
}