whyclj
2020-10-20 e9f26f7b367518b4087ff34b336aa3f2fbc0fd79
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.base;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
 
/**
 * Êý¾Ýת»»
 * @author LiJun
 *
 */
public class ComBase {
    
    
    public static byte changeIntToByte(int data)
    {
        return (byte)(data & 0xFF);
    }
    //------------------------------------------------------------------------------
    public static short changeIntToShort(int data)
    {
        return (short)(data & 0xFFFF);
    }
    //------------------------------------------------------------------------------
    public static byte changeShortToByte(short data)
    {
        return (byte)(data & 0xFF);
    }
    //------------------------------------------------------------------------------
    public static int changeByteToInt(byte data)
    {
        int tmp = data;
        return (tmp & 0xFF);
    }
    //------------------------------------------------------------------------------
    public static int changeShortToInt(short data)
    {
        int tmp = data;
        return (tmp & 0xFFFF);
    }
    //------------------------------------------------------------------------------
    public static double changeShortToDouble(short data)
    {
        int tmp = data & 0xFFFF;
        return (double)(tmp);
    }
    
    public static int ByteToHex(byte temp1 ,byte temp2) {  
        int Rtemp;
        Rtemp = ((temp1 &0xff)<<8) | (temp2 & 0xff);
        return (short)Rtemp;
    } 
     
 
    public static int ByteToHexxx(byte temp1 ,byte temp2) {
        int Rtemp;
        Rtemp = ((temp1 &0xff)<<8) | (temp2 & 0xff);
        return (int)Rtemp;
    } 
 
    public static int ByteToInt(byte temp1 ,byte temp2,byte temp3 ,byte temp4){
        int Rtemp; 
        int Rtemp1,Rtemp2;
        Rtemp = 0;
        Rtemp1 = ByteToHexxx( temp3 ,temp4);
        Rtemp2 = ByteToHexxx( temp1 ,temp2);
        Rtemp= (int)(Rtemp1<<16) +(int)(Rtemp2);
        return Rtemp;
    }
    
    public static long ByteToLong(byte temp1 ,byte temp2,byte temp3 ,byte temp4){
        long Rtemp; 
        long Rtemp1,Rtemp2;
        Rtemp = 0;
        Rtemp1 = ByteToHexxx( temp3 ,temp4);
        Rtemp2 = ByteToHexxx( temp1 ,temp2);
        Rtemp= (int)(Rtemp1<<16) +(int)(Rtemp2);
        return Rtemp;
    }
 
    
    public static byte[] IntToByte(int num){
        byte[]bytes=new byte[4];
        bytes[0]=(byte) ((num>>8)&0xff);
        bytes[1]=(byte) ((num)&0xff);
        bytes[2]=(byte) ((num>>24)&0xff);
        bytes[3]=(byte) (num>>16&0xff);
        return bytes;
    }
    
    public static byte[] LongToByte(long num){
        byte[]bytes=new byte[4];
        bytes[0]=(byte) ((num>>8)&0xff);
        bytes[1]=(byte) ((num)&0xff);
        bytes[2]=(byte) ((num>>24)&0xff);
        bytes[3]=(byte) (num>>16&0xff);
        return bytes;
    }
    
    public static int my_power_2(int N){
        StringBuffer v = new StringBuffer("");
        long num[] = new long[2];
        num[1] = 1;
        if(N > 62){
           num[0] = 1;
           num[0] = num[0]<<(N - 62);
           num[1] = num[1]<<62;
           String s = String.valueOf(num[1]);
           int size = 30,i = 0,j = 0;
           long n[] = new long[size + 1];
           //System.out.println(num[0]+" "+s);
           for(i = s.length() - 1;i >= 0;-- i){
               n[j ++] = (long) (num[0] * (s.charAt(i) - '0'));
               //System.out.println(n[j - 1]);
           }
           for(i = 0;i < size;++ i){
               while(n[i] > 9){
                   n[i + 1] += n[i] / 10;
                   n[i] %= 10;
               }
           }
           boolean bl = false;
           for(i = size;i >= 0;-- i){
               if(n[i] != 0 || bl){
                   v.append(n[i]);
                   bl = true;
               }
           }
        }else{
           num[1] = num[1] << N;
           v.append(String.valueOf(num[1]));
        }   
        return Integer.parseInt(v.toString());
    }
    
    /**
     *     »ñÈ¡Êý¾ÝÍ·µÄ³¤¶È
     * @param b
     * @return
     */
    public static int createPackHeadCount(byte[] b) {
        ByteBuffer bf = ByteBuffer.allocate(b.length);
        bf.order(ByteOrder.LITTLE_ENDIAN);
        bf.put(b);
        bf.flip();
        return ComBase.changeShortToInt(bf.getShort());
    }
    
    
    /**
     *     ·µ»Ø¼ÆËãºóµÄУÑéÊý×é
     * @param data
     * @return
     */
    public static byte[] calCheckSum(byte[] data) {
        if(data != null && data.length%2 == 0) {
            //·Ç¿Õ,²¢ÇÒ×Ö½ÚΪżÊý×Ö½Ú
            ByteBuffer bf = ByteBuffer.allocate(data.length+2);
            bf.order(ByteOrder.LITTLE_ENDIAN);
            bf.put(data);
            short sum = 0;
            bf.position(0);
            for(int i = 0;i<data.length/2;i++) {
                sum += bf.getShort()&0xFFFF;
            }
            bf.putShort((short) ((~sum)&0xFFFF));
            data = bf.array();
        }
        return data;
    }
    
    
    /**
     *     ¼ì²éÊý¾ÝУÑéºÍ
     * @param data
     * @return
     */
    public static boolean checkPackageSum(byte[] data) {
        boolean flag = false;
        if(data != null && data.length%2==0) {
            ByteBuffer bf = ByteBuffer.allocate(data.length);
            bf.order(ByteOrder.LITTLE_ENDIAN);
            bf.put(data);
            bf.flip();
            short sum = 0 ;
            for(int i = 0;i<bf.limit()/2;i++) {
                sum += bf.getShort()&0xFFFF;
            }
            if((sum&0xFFFF) == 0xFFFF) {
                flag = true;
            }
        }
        return flag;
    }
    
    /**
     *     ¼ì²éÊý¾ÝУÑéºÍ
     * @param buffer
     * @return
     */
    public static boolean checkByteBufferSum(ByteBuffer buffer) {
        boolean flag = false;
        if(buffer != null && buffer.limit()%2==0) {
            ByteBuffer bf = buffer;
            bf.order(ByteOrder.LITTLE_ENDIAN);
            bf.position(0);
            short sum = 0 ;
            for(int i = 0;i<bf.limit()/2;i++) {
                sum += bf.getShort()&0xFFFF;
            }
            if((sum&0xFFFF) == 0xFFFF) {
                flag = true;
            }
        }
        return flag;
    }
    
}