1
|
-package org.thingsboard.server.transport.tcp.util;
|
|
|
2
|
-
|
|
|
3
|
-import io.netty.buffer.ByteBuf;
|
|
|
4
|
-import org.apache.commons.lang3.ArrayUtils;
|
|
|
5
|
-
|
|
|
6
|
-import java.io.*;
|
|
|
7
|
-import java.nio.charset.Charset;
|
|
|
8
|
-import java.util.regex.Pattern;
|
|
|
9
|
-
|
|
|
10
|
-/**
|
|
|
11
|
- * @Description :数据类型转换工具类
|
|
|
12
|
- * 二进制的最高位是符号位,0表示正,1表示负。
|
|
|
13
|
- * 负数采用二进制的补码表示,10进制转为二进制得到的是源码,将源码按位取反得到的是反码,反码加1得到补码
|
|
|
14
|
- *
|
|
|
15
|
- *
|
|
|
16
|
- * *¥¥¥【基本数据类型】¥¥¥¥¥¥¥¥¥¥¥¥
|
|
|
17
|
- 1/8:boolean【1 位】
|
|
|
18
|
- 3/8:byte 【1个字节,8 位】
|
|
|
19
|
- 3/8:short 【2个字节,16位】
|
|
|
20
|
- 4/8:int 【4个字节,32位】
|
|
|
21
|
- 5/8:long 【8个字节,64位】
|
|
|
22
|
- 6/8:float 【4个字节,32位】
|
|
|
23
|
- 7/8:double 【8个字节,64位】
|
|
|
24
|
- 8/8:chart 【2个字节,16位】
|
|
|
25
|
- *
|
|
|
26
|
- *
|
|
|
27
|
- *
|
|
|
28
|
- * *¥¥¥【位运算】¥¥¥¥¥¥¥¥¥¥¥¥
|
|
|
29
|
- 左移【<< n】:向左移动,左边高位舍弃,右边的低位补0,例如:2左移3位,结果为16
|
|
|
30
|
- 右移【>>n】:向右移动,右边的舍弃掉,左边补的值取决于原来最高位,原来是1就补1,原来是0就补0
|
|
|
31
|
- 无符号右移【>>>n】向右移动,右边的舍弃掉,左边补0。
|
|
|
32
|
- *
|
|
|
33
|
- *
|
|
|
34
|
- *
|
|
|
35
|
- * 1.其它地方抛出异常,交由控制层统一处理
|
|
|
36
|
- * 2.服务层注意持久化的事务管理
|
|
|
37
|
- * @Author: junlianglee
|
|
|
38
|
- * @Date Created in 2019/12/11.
|
|
|
39
|
- * @Modified by Administrator on 2019/12/11.
|
|
|
40
|
- */
|
|
|
41
|
-public class ByteUtils {
|
|
|
42
|
- public static final char[] ascii = "0123456789ABCDEF".toCharArray();
|
|
|
43
|
- private static char[] HEX_VOCABLE = {'0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
44
|
- '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
|
|
45
|
-
|
|
|
46
|
- public static final String UTF_8 = "UTF-8";
|
|
|
47
|
-
|
|
|
48
|
- private static String hexString="0123456789ABCDEF";
|
|
|
49
|
- /**
|
|
|
50
|
- * 将short整型数值转换为字节数组
|
|
|
51
|
- *
|
|
|
52
|
- * @param data
|
|
|
53
|
- * @return
|
|
|
54
|
- */
|
|
|
55
|
- public static byte[] getBytes(short data) {
|
|
|
56
|
- byte[] bytes = new byte[2];
|
|
|
57
|
- bytes[0] = (byte) ((data & 0xff00) >> 8);//1个十六进制位匹配4个二进制位
|
|
|
58
|
- bytes[1] = (byte) (data & 0xff);
|
|
|
59
|
- return bytes;
|
|
|
60
|
- }
|
|
|
61
|
-
|
|
|
62
|
- /**
|
|
|
63
|
- * 将字符转换为字节数组
|
|
|
64
|
- *
|
|
|
65
|
- * @param data
|
|
|
66
|
- * @return
|
|
|
67
|
- */
|
|
|
68
|
- public static byte[] getBytes(char data) {
|
|
|
69
|
- byte[] bytes = new byte[2];
|
|
|
70
|
- bytes[0] = (byte) (data >> 8);
|
|
|
71
|
- bytes[1] = (byte) (data);
|
|
|
72
|
- return bytes;
|
|
|
73
|
- }
|
|
|
74
|
-
|
|
|
75
|
- /**
|
|
|
76
|
- * 将布尔值转换为字节数组
|
|
|
77
|
- *
|
|
|
78
|
- * @param data
|
|
|
79
|
- * @return
|
|
|
80
|
- */
|
|
|
81
|
- public static byte[] getBytes(boolean data) {
|
|
|
82
|
- byte[] bytes = new byte[1];
|
|
|
83
|
- bytes[0] = (byte) (data ? 1 : 0);
|
|
|
84
|
- return bytes;
|
|
|
85
|
- }
|
|
|
86
|
-
|
|
|
87
|
- /**
|
|
|
88
|
- * 将整型数值转换为字节数组
|
|
|
89
|
- *
|
|
|
90
|
- * @param data
|
|
|
91
|
- * @return
|
|
|
92
|
- */
|
|
|
93
|
- public static byte[] getBytes(int data) {
|
|
|
94
|
- byte[] bytes = new byte[4];
|
|
|
95
|
- bytes[0] = (byte) ((data & 0xff000000) >> 24);
|
|
|
96
|
- bytes[1] = (byte) ((data & 0xff0000) >> 16);
|
|
|
97
|
- bytes[2] = (byte) ((data & 0xff00) >> 8);
|
|
|
98
|
- bytes[3] = (byte) (data & 0xff);
|
|
|
99
|
- return bytes;
|
|
|
100
|
- }
|
|
|
101
|
-
|
|
|
102
|
- /**
|
|
|
103
|
- * 将long整型数值转换为字节数组
|
|
|
104
|
- *
|
|
|
105
|
- * @param data
|
|
|
106
|
- * @return
|
|
|
107
|
- */
|
|
|
108
|
- public static byte[] getBytes(long data) {
|
|
|
109
|
- byte[] bytes = new byte[8];
|
|
|
110
|
- bytes[0] = (byte) ((data >> 56) & 0xff);
|
|
|
111
|
- bytes[1] = (byte) ((data >> 48) & 0xff);
|
|
|
112
|
- bytes[2] = (byte) ((data >> 40) & 0xff);
|
|
|
113
|
- bytes[3] = (byte) ((data >> 32) & 0xff);
|
|
|
114
|
- bytes[4] = (byte) ((data >> 24) & 0xff);
|
|
|
115
|
- bytes[5] = (byte) ((data >> 16) & 0xff);
|
|
|
116
|
- bytes[6] = (byte) ((data >> 8) & 0xff);
|
|
|
117
|
- bytes[7] = (byte) (data & 0xff);
|
|
|
118
|
- return bytes;
|
|
|
119
|
- }
|
|
|
120
|
-
|
|
|
121
|
- /**
|
|
|
122
|
- * 将float型数值转换为字节数组
|
|
|
123
|
- *
|
|
|
124
|
- * @param data
|
|
|
125
|
- * @return
|
|
|
126
|
- */
|
|
|
127
|
- public static byte[] getBytes(float data) {
|
|
|
128
|
- int intBits = Float.floatToIntBits(data);
|
|
|
129
|
- return getBytes(intBits);
|
|
|
130
|
- }
|
|
|
131
|
-
|
|
|
132
|
- /**
|
|
|
133
|
- * 将double型数值转换为字节数组
|
|
|
134
|
- *
|
|
|
135
|
- * @param data
|
|
|
136
|
- * @return
|
|
|
137
|
- */
|
|
|
138
|
- public static byte[] getBytes(double data) {
|
|
|
139
|
- long intBits = Double.doubleToLongBits(data);
|
|
|
140
|
- return getBytes(intBits);
|
|
|
141
|
- }
|
|
|
142
|
-
|
|
|
143
|
- /**
|
|
|
144
|
- * 将字符串按照charsetName编码格式的字节数组
|
|
|
145
|
- *
|
|
|
146
|
- * @param data
|
|
|
147
|
- * 字符串
|
|
|
148
|
- * @param charsetName
|
|
|
149
|
- * 编码格式
|
|
|
150
|
- * @return
|
|
|
151
|
- */
|
|
|
152
|
- public static byte[] getBytes(String data, String charsetName) {
|
|
|
153
|
- Charset charset = Charset.forName(charsetName);
|
|
|
154
|
- return data.getBytes(charset);
|
|
|
155
|
- }
|
|
|
156
|
-
|
|
|
157
|
-
|
|
|
158
|
-
|
|
|
159
|
-
|
|
|
160
|
-
|
|
|
161
|
- /**
|
|
|
162
|
- * 将字节数组前8字节转换为long整型数值
|
|
|
163
|
- *
|
|
|
164
|
- * @param bytes
|
|
|
165
|
- * @return
|
|
|
166
|
- */
|
|
|
167
|
- public static long getLong(byte[] bytes) {
|
|
|
168
|
- return (0xff00000000000000L & ((long) bytes[0] << 56)
|
|
|
169
|
- | (0xff000000000000L & ((long) bytes[1] << 48))
|
|
|
170
|
- | (0xff0000000000L & ((long) bytes[2] << 40))
|
|
|
171
|
- | (0xff00000000L & ((long) bytes[3] << 32))
|
|
|
172
|
- | (0xff000000L & ((long) bytes[4] << 24))
|
|
|
173
|
- | (0xff0000L & ((long) bytes[5] << 16))
|
|
|
174
|
- | (0xff00L & ((long) bytes[6] << 8))
|
|
|
175
|
- | (0xffL & (long) bytes[7]));
|
|
|
176
|
- }
|
|
|
177
|
-
|
|
|
178
|
-
|
|
|
179
|
-
|
|
|
180
|
- /**
|
|
|
181
|
- * 将charsetName编码格式的字节数组转换为字符串
|
|
|
182
|
- *
|
|
|
183
|
- * @param bytes
|
|
|
184
|
- * @param charsetName
|
|
|
185
|
- * @return
|
|
|
186
|
- */
|
|
|
187
|
- public static String getString(byte[] bytes, String charsetName) {
|
|
|
188
|
- return new String(bytes, Charset.forName(charsetName));
|
|
|
189
|
- }
|
|
|
190
|
-
|
|
|
191
|
-
|
|
|
192
|
- public static byte[] hexStr2Bytes(String hexString) {
|
|
|
193
|
- if (hexString == null || hexString.equals("")) {
|
|
|
194
|
- return null;
|
|
|
195
|
- }
|
|
|
196
|
- // toUpperCase将字符串中的所有字符转换为大写
|
|
|
197
|
- hexString = hexString.toUpperCase();
|
|
|
198
|
- int length = hexString.length() / 2;// 2个16进制字符表示一个字节,所以字节数组长度是字符串长度除以2
|
|
|
199
|
- // toCharArray将此字符串转换为一个新的字符数组。
|
|
|
200
|
- char[] hexChars = hexString.toCharArray();
|
|
|
201
|
- byte[] d = new byte[length];
|
|
|
202
|
- for (int i = 0; i < length; i++) {
|
|
|
203
|
- int pos = i * 2;
|
|
|
204
|
- d[i] = (byte) (charToByte(hexChars[pos]) << 4
|
|
|
205
|
- | charToByte(hexChars[pos + 1]));
|
|
|
206
|
- }
|
|
|
207
|
- return d;
|
|
|
208
|
- }
|
|
|
209
|
-
|
|
|
210
|
- // charToByte返回在指定字符的第一个发生的字符串中的索引,即返回匹配字符
|
|
|
211
|
- private static byte charToByte(char c) {
|
|
|
212
|
- return (byte) "0123456789ABCDEF".indexOf(c);
|
|
|
213
|
- }
|
|
|
214
|
- /**
|
|
|
215
|
- * 将16进制字符串转换为字节数组
|
|
|
216
|
- *
|
|
|
217
|
- * @param hex
|
|
|
218
|
- * @return
|
|
|
219
|
- */
|
|
|
220
|
- public static byte[] hexToBytes(String hex) {
|
|
|
221
|
- if (hex.length() % 2 != 0)
|
|
|
222
|
- throw new IllegalArgumentException(
|
|
|
223
|
- "input string should be any multiple of 2!");
|
|
|
224
|
- hex.toUpperCase();
|
|
|
225
|
-
|
|
|
226
|
- byte[] byteBuffer = new byte[hex.length() / 2];
|
|
|
227
|
-
|
|
|
228
|
- byte padding = 0x00;
|
|
|
229
|
- boolean paddingTurning = false;
|
|
|
230
|
- for (int i = 0; i < hex.length(); i++) {
|
|
|
231
|
- if (paddingTurning) {
|
|
|
232
|
- char c = hex.charAt(i);
|
|
|
233
|
- int index = indexOf(hex, c);
|
|
|
234
|
- padding = (byte) ((padding << 4) | index);
|
|
|
235
|
- byteBuffer[i / 2] = padding;
|
|
|
236
|
- padding = 0x00;
|
|
|
237
|
- paddingTurning = false;
|
|
|
238
|
- } else {
|
|
|
239
|
- char c = hex.charAt(i);
|
|
|
240
|
- int index = indexOf(hex, c);
|
|
|
241
|
- padding = (byte) (padding | index);
|
|
|
242
|
- paddingTurning = true;
|
|
|
243
|
- }
|
|
|
244
|
-
|
|
|
245
|
- }
|
|
|
246
|
- return byteBuffer;
|
|
|
247
|
- }
|
|
|
248
|
-
|
|
|
249
|
- private static int indexOf(String input, char c) {
|
|
|
250
|
- int index = ArrayUtils.indexOf(HEX_VOCABLE, c);
|
|
|
251
|
-
|
|
|
252
|
- if (index < 0) {
|
|
|
253
|
- throw new IllegalArgumentException("err input:" + input);
|
|
|
254
|
- }
|
|
|
255
|
- return index;
|
|
|
256
|
-
|
|
|
257
|
- }
|
|
|
258
|
- /**
|
|
|
259
|
- * 字节数组转16进制字符串
|
|
|
260
|
- *
|
|
|
261
|
- * @param bs
|
|
|
262
|
- * @return
|
|
|
263
|
- */
|
|
|
264
|
- public static String bytesToHex(byte[] bs) {
|
|
|
265
|
- StringBuilder sb = new StringBuilder();
|
|
|
266
|
- for (byte b : bs) {
|
|
|
267
|
- int high = (b >> 4) & 0x0f;
|
|
|
268
|
- int low = b & 0x0f;
|
|
|
269
|
- sb.append(HEX_VOCABLE[high]);
|
|
|
270
|
- sb.append(HEX_VOCABLE[low]);
|
|
|
271
|
- }
|
|
|
272
|
- return sb.toString();
|
|
|
273
|
- }
|
|
|
274
|
-
|
|
|
275
|
- private static final String messageRegular = "^[A-Za-z0-9!@#$%^&*()]+$";
|
|
|
276
|
- public static String bytesToStr(byte[] bs){
|
|
|
277
|
- String result = getString(bs, ByteUtils.UTF_8);
|
|
|
278
|
- if(!Pattern.compile(messageRegular).matcher(result).matches()){
|
|
|
279
|
- result = bytesToHex(bs);
|
|
|
280
|
- }
|
|
|
281
|
- return result;
|
|
|
282
|
- }
|
|
|
283
|
-
|
|
|
284
|
- public static byte[] strToBytes(String str){
|
|
|
285
|
- if(Pattern.compile("^[A-Fa-f0-9]+$").matcher(str).matches()){
|
|
|
286
|
- return hexStr2Bytes(str);
|
|
|
287
|
- }
|
|
|
288
|
- return getBytes(str,ByteUtils.UTF_8);
|
|
|
289
|
- }
|
|
|
290
|
-
|
|
|
291
|
- /**
|
|
|
292
|
- * 字节数组取前len个字节转16进制字符串
|
|
|
293
|
- *
|
|
|
294
|
- * @param bs
|
|
|
295
|
- * @param len
|
|
|
296
|
- * @return
|
|
|
297
|
- */
|
|
|
298
|
- public static String bytesToHex(byte[] bs, int len) {
|
|
|
299
|
- StringBuilder sb = new StringBuilder();
|
|
|
300
|
- for (int i = 0; i < len; i++) {
|
|
|
301
|
- byte b = bs[i];
|
|
|
302
|
- int high = (b >> 4) & 0x0f;
|
|
|
303
|
- int low = b & 0x0f;
|
|
|
304
|
- sb.append(HEX_VOCABLE[high]);
|
|
|
305
|
- sb.append(HEX_VOCABLE[low]);
|
|
|
306
|
- }
|
|
|
307
|
- return sb.toString();
|
|
|
308
|
- }
|
|
|
309
|
- /**
|
|
|
310
|
- * 字节数组偏移offset长度之后的取len个字节转16进制字符串
|
|
|
311
|
- *
|
|
|
312
|
- * @param bs
|
|
|
313
|
- * @param offset
|
|
|
314
|
- * @param len
|
|
|
315
|
- * @return
|
|
|
316
|
- */
|
|
|
317
|
- public static String bytesToHex(byte[] bs, int offset, int len) {
|
|
|
318
|
- StringBuilder sb = new StringBuilder();
|
|
|
319
|
- for (int i = 0; i < len; i++) {
|
|
|
320
|
- byte b = bs[offset + i];
|
|
|
321
|
- int high = (b >> 4) & 0x0f;
|
|
|
322
|
- int low = b & 0x0f;
|
|
|
323
|
- sb.append(HEX_VOCABLE[high]);
|
|
|
324
|
- sb.append(HEX_VOCABLE[low]);
|
|
|
325
|
- }
|
|
|
326
|
- return sb.toString();
|
|
|
327
|
- }
|
|
|
328
|
- /**
|
|
|
329
|
- * 字节数组转16进制字符串
|
|
|
330
|
- *
|
|
|
331
|
- * @param bs
|
|
|
332
|
- * @return
|
|
|
333
|
- */
|
|
|
334
|
- public static String byteToHex(byte bs) {
|
|
|
335
|
- StringBuilder sb = new StringBuilder();
|
|
|
336
|
- int high = (bs >> 4) & 0x0f;
|
|
|
337
|
- int low = bs & 0x0f;
|
|
|
338
|
- sb.append(HEX_VOCABLE[high]);
|
|
|
339
|
- sb.append(HEX_VOCABLE[low]);
|
|
|
340
|
- return sb.toString();
|
|
|
341
|
- }
|
|
|
342
|
- /**
|
|
|
343
|
- * 将字节数组取反
|
|
|
344
|
- *
|
|
|
345
|
- * @param src
|
|
|
346
|
- * @return
|
|
|
347
|
- */
|
|
|
348
|
- public static String negate(byte[] src) {
|
|
|
349
|
- if (src == null || src.length == 0) {
|
|
|
350
|
- return null;
|
|
|
351
|
- }
|
|
|
352
|
- byte[] temp = new byte[2 * src.length];
|
|
|
353
|
- for (int i = 0; i < src.length; i++) {
|
|
|
354
|
- byte tmp = (byte) (0xFF ^ src[i]);
|
|
|
355
|
- temp[i * 2] = (byte) ((tmp >> 4) & 0x0f);
|
|
|
356
|
- temp[i * 2 + 1] = (byte) (tmp & 0x0f);
|
|
|
357
|
- }
|
|
|
358
|
- StringBuffer res = new StringBuffer();
|
|
|
359
|
- for (int i = 0; i < temp.length; i++) {
|
|
|
360
|
- res.append(ascii[temp[i]]);
|
|
|
361
|
- }
|
|
|
362
|
- return res.toString();
|
|
|
363
|
- }
|
|
|
364
|
-
|
|
|
365
|
-
|
|
|
366
|
-
|
|
|
367
|
- /**
|
|
|
368
|
- * 数组合并
|
|
|
369
|
- * @param before
|
|
|
370
|
- * @param end
|
|
|
371
|
- * @return
|
|
|
372
|
- */
|
|
|
373
|
- public static byte[] merge(byte[]before,byte[]end){
|
|
|
374
|
- byte[] result= new byte[before.length+end.length];
|
|
|
375
|
- System.arraycopy(before,0,result,0,before.length);
|
|
|
376
|
- System.arraycopy(end,0,result,before.length,end.length);
|
|
|
377
|
- return result;
|
|
|
378
|
- }
|
|
|
379
|
-
|
|
|
380
|
- public static byte[] subBytes(byte[]before,Integer start,Integer length){
|
|
|
381
|
- byte[] result= new byte[length];
|
|
|
382
|
- System.arraycopy(before,start,result,0,length);
|
|
|
383
|
- return result;
|
|
|
384
|
- }
|
|
|
385
|
-
|
|
|
386
|
- /**
|
|
|
387
|
- * 比较字节数组是否相同
|
|
|
388
|
- *
|
|
|
389
|
- * @param a
|
|
|
390
|
- * @param b
|
|
|
391
|
- * @return
|
|
|
392
|
- */
|
|
|
393
|
- public static boolean compareBytes(byte[] a, byte[] b) {
|
|
|
394
|
- if (a == null || a.length == 0 || b == null || b.length == 0
|
|
|
395
|
- || a.length != b.length) {
|
|
|
396
|
- return false;
|
|
|
397
|
- }
|
|
|
398
|
- if (a.length == b.length) {
|
|
|
399
|
- for (int i = 0; i < a.length; i++) {
|
|
|
400
|
- if (a[i] != b[i]) {
|
|
|
401
|
- return false;
|
|
|
402
|
- }
|
|
|
403
|
- }
|
|
|
404
|
- } else {
|
|
|
405
|
- return false;
|
|
|
406
|
- }
|
|
|
407
|
- return true;
|
|
|
408
|
- }
|
|
|
409
|
- /**
|
|
|
410
|
- * 只比对指定长度byte
|
|
|
411
|
- *
|
|
|
412
|
- * @param a
|
|
|
413
|
- * @param b
|
|
|
414
|
- * @param len
|
|
|
415
|
- * @return
|
|
|
416
|
- */
|
|
|
417
|
- public static boolean compareBytes(byte[] a, byte[] b, int len) {
|
|
|
418
|
- if (a == null || a.length == 0 || b == null || b.length == 0
|
|
|
419
|
- || a.length < len || b.length < len) {
|
|
|
420
|
- return false;
|
|
|
421
|
- }
|
|
|
422
|
- for (int i = 0; i < len; i++) {
|
|
|
423
|
- if (a[i] != b[i]) {
|
|
|
424
|
- return false;
|
|
|
425
|
- }
|
|
|
426
|
- }
|
|
|
427
|
- return true;
|
|
|
428
|
- }
|
|
|
429
|
-
|
|
|
430
|
- /**
|
|
|
431
|
- * 将字节数组转换为二进制字符串
|
|
|
432
|
- *
|
|
|
433
|
- * @param items
|
|
|
434
|
- * @return
|
|
|
435
|
- */
|
|
|
436
|
- public static String bytesToBinaryString(byte[] items) {
|
|
|
437
|
- if (items == null || items.length == 0) {
|
|
|
438
|
- return null;
|
|
|
439
|
- }
|
|
|
440
|
- StringBuffer buf = new StringBuffer();
|
|
|
441
|
- for (byte item : items) {
|
|
|
442
|
- buf.append(byteToBinaryString(item));
|
|
|
443
|
- }
|
|
|
444
|
- return buf.toString();
|
|
|
445
|
- }
|
|
|
446
|
-
|
|
|
447
|
- /**
|
|
|
448
|
- * 将字节转换为二进制字符串
|
|
|
449
|
- *
|
|
|
450
|
- * @param item
|
|
|
451
|
- * @return
|
|
|
452
|
- */
|
|
|
453
|
- public static String byteToBinaryString(byte item) {
|
|
|
454
|
- byte a = item;
|
|
|
455
|
- StringBuffer buf = new StringBuffer();
|
|
|
456
|
- for (int i = 0; i < 8; i++) {
|
|
|
457
|
- buf.insert(0, a % 2);
|
|
|
458
|
- a = (byte) (a >> 1);
|
|
|
459
|
- }
|
|
|
460
|
- return buf.toString();
|
|
|
461
|
- }
|
|
|
462
|
-
|
|
|
463
|
- /**
|
|
|
464
|
- * 对数组a,b进行异或运算
|
|
|
465
|
- *
|
|
|
466
|
- * @param a
|
|
|
467
|
- * @param b
|
|
|
468
|
- * @return
|
|
|
469
|
- */
|
|
|
470
|
- public static byte[] xor(byte[] a, byte[] b) {
|
|
|
471
|
- if (a == null || a.length == 0 || b == null || b.length == 0
|
|
|
472
|
- || a.length != b.length) {
|
|
|
473
|
- return null;
|
|
|
474
|
- }
|
|
|
475
|
- byte[] result = new byte[a.length];
|
|
|
476
|
- for (int i = 0; i < a.length; i++) {
|
|
|
477
|
- result[i] = (byte) (a[i] ^ b[i]);
|
|
|
478
|
- }
|
|
|
479
|
- return result;
|
|
|
480
|
- }
|
|
|
481
|
-
|
|
|
482
|
- /**
|
|
|
483
|
- * 对数组a,b进行异或运算 运算长度len
|
|
|
484
|
- *
|
|
|
485
|
- * @param a
|
|
|
486
|
- * @param b
|
|
|
487
|
- * @param len
|
|
|
488
|
- * @return
|
|
|
489
|
- */
|
|
|
490
|
- public static byte[] xor(byte[] a, byte[] b, int len) {
|
|
|
491
|
- if (a == null || a.length == 0 || b == null || b.length == 0) {
|
|
|
492
|
- return null;
|
|
|
493
|
- }
|
|
|
494
|
- if (a.length < len || b.length < len) {
|
|
|
495
|
- return null;
|
|
|
496
|
- }
|
|
|
497
|
- byte[] result = new byte[len];
|
|
|
498
|
- for (int i = 0; i < len; i++) {
|
|
|
499
|
- result[i] = (byte) (a[i] ^ b[i]);
|
|
|
500
|
- }
|
|
|
501
|
- return result;
|
|
|
502
|
- }
|
|
|
503
|
- /**
|
|
|
504
|
- * 将short整型数值转换为字节数组
|
|
|
505
|
- *
|
|
|
506
|
- * @param num
|
|
|
507
|
- * @return
|
|
|
508
|
- */
|
|
|
509
|
- public static byte[] shortToBytes(int num) {
|
|
|
510
|
- byte[] temp = new byte[2];
|
|
|
511
|
- for (int i = 0; i < 2; i++) {
|
|
|
512
|
- temp[i] = (byte) ((num >>> (8 - i * 8)) & 0xFF);
|
|
|
513
|
- }
|
|
|
514
|
- return temp;
|
|
|
515
|
- }
|
|
|
516
|
-
|
|
|
517
|
- /**
|
|
|
518
|
- * 将字节数组转为整型
|
|
|
519
|
- *
|
|
|
520
|
- * @param arr
|
|
|
521
|
- * @return
|
|
|
522
|
- */
|
|
|
523
|
- public static int bytesToShort(byte[] arr) {
|
|
|
524
|
- int mask = 0xFF;
|
|
|
525
|
- int temp = 0;
|
|
|
526
|
- int result = 0;
|
|
|
527
|
- for (int i = 0; i < 2; i++) {
|
|
|
528
|
- result <<= 8;
|
|
|
529
|
- temp = arr[i] & mask;
|
|
|
530
|
- result |= temp;
|
|
|
531
|
- }
|
|
|
532
|
- return result;
|
|
|
533
|
- }
|
|
|
534
|
-
|
|
|
535
|
- /**
|
|
|
536
|
- * 将整型数值转换为指定长度的字节数组
|
|
|
537
|
- *
|
|
|
538
|
- * @param num
|
|
|
539
|
- * @return
|
|
|
540
|
- */
|
|
|
541
|
- public static byte[] intToBytes(int num) {
|
|
|
542
|
- byte[] temp = new byte[4];
|
|
|
543
|
- for (int i = 0; i < 4; i++) {
|
|
|
544
|
- temp[i] = (byte) ((num >>> (24 - i * 8)) & 0xFF);
|
|
|
545
|
- }
|
|
|
546
|
- return temp;
|
|
|
547
|
- }
|
|
|
548
|
-
|
|
|
549
|
- /**
|
|
|
550
|
- * 将整型数值转换为指定长度的字节数组
|
|
|
551
|
- *
|
|
|
552
|
- * @param src
|
|
|
553
|
- * @param len
|
|
|
554
|
- * @return
|
|
|
555
|
- */
|
|
|
556
|
- public static byte[] intToBytes(int src, int len) {
|
|
|
557
|
- if (len < 1 || len > 4) {
|
|
|
558
|
- return null;
|
|
|
559
|
- }
|
|
|
560
|
- byte[] temp = new byte[len];
|
|
|
561
|
- for (int i = 0; i < len; i++) {
|
|
|
562
|
- temp[len - 1 - i] = (byte) ((src >>> (8 * i)) & 0xFF);
|
|
|
563
|
- }
|
|
|
564
|
- return temp;
|
|
|
565
|
- }
|
|
|
566
|
-
|
|
|
567
|
- /**
|
|
|
568
|
- * 将字节数组转换为整型数值
|
|
|
569
|
- *
|
|
|
570
|
- * @param arr
|
|
|
571
|
- * @return
|
|
|
572
|
- */
|
|
|
573
|
- public static int bytesToInt(byte[] arr) {
|
|
|
574
|
- int mask = 0xFF;
|
|
|
575
|
- int temp = 0;
|
|
|
576
|
- int result = 0;
|
|
|
577
|
- for (int i = 0; i < 4; i++) {
|
|
|
578
|
- result <<= 8;
|
|
|
579
|
- temp = arr[i] & mask;
|
|
|
580
|
- result |= temp;
|
|
|
581
|
- }
|
|
|
582
|
- return result;
|
|
|
583
|
- }
|
|
|
584
|
-
|
|
|
585
|
- /**
|
|
|
586
|
- * 将long整型数值转换为字节数组
|
|
|
587
|
- *
|
|
|
588
|
- * @param num
|
|
|
589
|
- * @return
|
|
|
590
|
- */
|
|
|
591
|
- public static byte[] longToBytes(long num) {
|
|
|
592
|
- byte[] temp = new byte[8];
|
|
|
593
|
- for (int i = 0; i < 8; i++) {
|
|
|
594
|
- temp[i] = (byte) ((num >>> (56 - i * 8)) & 0xFF);
|
|
|
595
|
- }
|
|
|
596
|
- return temp;
|
|
|
597
|
- }
|
|
|
598
|
-
|
|
|
599
|
- /**
|
|
|
600
|
- * 将字节数组转换为long整型数值
|
|
|
601
|
- *
|
|
|
602
|
- * @param arr
|
|
|
603
|
- * @return
|
|
|
604
|
- */
|
|
|
605
|
- public static long bytesToLong(byte[] arr) {
|
|
|
606
|
- int mask = 0xFF;
|
|
|
607
|
- int temp = 0;
|
|
|
608
|
- long result = 0;
|
|
|
609
|
- int len = Math.min(8, arr.length);
|
|
|
610
|
- for (int i = 0; i < len; i++) {
|
|
|
611
|
- result <<= 8;
|
|
|
612
|
- temp = arr[i] & mask;
|
|
|
613
|
- result |= temp;
|
|
|
614
|
- }
|
|
|
615
|
- return result;
|
|
|
616
|
- }
|
|
|
617
|
-
|
|
|
618
|
- /**
|
|
|
619
|
- * 将16进制字符转换为字节
|
|
|
620
|
- *
|
|
|
621
|
- * @param c
|
|
|
622
|
- * @return
|
|
|
623
|
- */
|
|
|
624
|
- public static byte toByte(char c) {
|
|
|
625
|
- byte b = (byte) "0123456789ABCDEF".indexOf(c);
|
|
|
626
|
- return b;
|
|
|
627
|
- }
|
|
|
628
|
-
|
|
|
629
|
- /**
|
|
|
630
|
- * 功能描述:把两个字节的字节数组转化为整型数据,高位补零,例如:<br/>
|
|
|
631
|
- * 有字节数组byte[] data = new byte[]{1,2};转换后int数据的字节分布如下:<br/>
|
|
|
632
|
- * 00000000 00000000 00000001 00000010,函数返回258
|
|
|
633
|
- *
|
|
|
634
|
- * @param lenData
|
|
|
635
|
- * 需要进行转换的字节数组
|
|
|
636
|
- * @return 字节数组所表示整型值的大小
|
|
|
637
|
- */
|
|
|
638
|
- public static int bytesToIntWhereByteLengthEquals2(byte lenData[]) {
|
|
|
639
|
- if (lenData.length != 2) {
|
|
|
640
|
- return -1;
|
|
|
641
|
- }
|
|
|
642
|
- byte fill[] = new byte[]{0, 0};
|
|
|
643
|
- byte real[] = new byte[4];
|
|
|
644
|
- System.arraycopy(fill, 0, real, 0, 2);
|
|
|
645
|
- System.arraycopy(lenData, 0, real, 2, 2);
|
|
|
646
|
- int len = byteToInt(real);
|
|
|
647
|
- return len;
|
|
|
648
|
-
|
|
|
649
|
- }
|
|
|
650
|
-
|
|
|
651
|
- /**
|
|
|
652
|
- * 功能描述:将byte数组转化为int类型的数据
|
|
|
653
|
- *
|
|
|
654
|
- * @param byteVal
|
|
|
655
|
- * 需要转化的字节数组
|
|
|
656
|
- * @return 字节数组所表示的整型数据
|
|
|
657
|
- */
|
|
|
658
|
- public static int byteToInt(byte[] byteVal) {
|
|
|
659
|
- int result = 0;
|
|
|
660
|
- for (int i = 0; i < byteVal.length; i++) {
|
|
|
661
|
- int tmpVal = (byteVal[i] << (8 * (3 - i)));
|
|
|
662
|
- switch (i) {
|
|
|
663
|
- case 0 :
|
|
|
664
|
- tmpVal = tmpVal & 0xFF000000;
|
|
|
665
|
- break;
|
|
|
666
|
- case 1 :
|
|
|
667
|
- tmpVal = tmpVal & 0x00FF0000;
|
|
|
668
|
- break;
|
|
|
669
|
- case 2 :
|
|
|
670
|
- tmpVal = tmpVal & 0x0000FF00;
|
|
|
671
|
- break;
|
|
|
672
|
- case 3 :
|
|
|
673
|
- tmpVal = tmpVal & 0x000000FF;
|
|
|
674
|
- break;
|
|
|
675
|
- }
|
|
|
676
|
-
|
|
|
677
|
- result = result | tmpVal;
|
|
|
678
|
- }
|
|
|
679
|
- return result;
|
|
|
680
|
- }
|
|
|
681
|
- public static byte CheckXORSum(byte[] bData) {
|
|
|
682
|
- byte sum = 0x00;
|
|
|
683
|
- for (int i = 0; i < bData.length; i++) {
|
|
|
684
|
- sum ^= bData[i];
|
|
|
685
|
- }
|
|
|
686
|
- return sum;
|
|
|
687
|
- }
|
|
|
688
|
- /**
|
|
|
689
|
- * 从offset开始 将后续长度为len的byte字节转为int
|
|
|
690
|
- *
|
|
|
691
|
- * @param data
|
|
|
692
|
- * @param offset
|
|
|
693
|
- * @param len
|
|
|
694
|
- * @return
|
|
|
695
|
- */
|
|
|
696
|
- public static int bytesToInt(byte[] data, int offset, int len) {
|
|
|
697
|
- int mask = 0xFF;
|
|
|
698
|
- int temp = 0;
|
|
|
699
|
- int result = 0;
|
|
|
700
|
- len = Math.min(len, 4);
|
|
|
701
|
- for (int i = 0; i < len; i++) {
|
|
|
702
|
- result <<= 8;
|
|
|
703
|
- temp = data[offset + i] & mask;
|
|
|
704
|
- result |= temp;
|
|
|
705
|
- }
|
|
|
706
|
- return result;
|
|
|
707
|
- }
|
|
|
708
|
-
|
|
|
709
|
- /**
|
|
|
710
|
- * byte字节数组中的字符串的长度
|
|
|
711
|
- *
|
|
|
712
|
- * @param data
|
|
|
713
|
- * @return
|
|
|
714
|
- */
|
|
|
715
|
- public static int getBytesStringLen(byte[] data) {
|
|
|
716
|
- int count = 0;
|
|
|
717
|
- for (byte b : data) {
|
|
|
718
|
- if (b == 0x00)
|
|
|
719
|
- break;
|
|
|
720
|
- count++;
|
|
|
721
|
- }
|
|
|
722
|
- return count;
|
|
|
723
|
- }
|
|
|
724
|
-
|
|
|
725
|
- /**
|
|
|
726
|
- * 校验和
|
|
|
727
|
- *
|
|
|
728
|
- * @param msg
|
|
|
729
|
- * 需要计算校验和的byte数组
|
|
|
730
|
- * @param length
|
|
|
731
|
- * 校验和位数
|
|
|
732
|
- * @return 计算出的校验和数组
|
|
|
733
|
- */
|
|
|
734
|
- public static byte[] SumCheck(byte[] msg, int length) {
|
|
|
735
|
- long mSum = 0;
|
|
|
736
|
- byte[] mByte = new byte[length];
|
|
|
737
|
-
|
|
|
738
|
- /** 逐Byte添加位数和 */
|
|
|
739
|
- for (byte byteMsg : msg) {
|
|
|
740
|
- long mNum = ((long) byteMsg >= 0)
|
|
|
741
|
- ? (long) byteMsg
|
|
|
742
|
- : ((long) byteMsg + 256);
|
|
|
743
|
- mSum += mNum;
|
|
|
744
|
- } /** end of for (byte byteMsg : msg) */
|
|
|
745
|
-
|
|
|
746
|
- /** 位数和转化为Byte数组 */
|
|
|
747
|
- for (int liv_Count = 0; liv_Count < length; liv_Count++) {
|
|
|
748
|
- mByte[length - liv_Count
|
|
|
749
|
- - 1] = (byte) (mSum >> (liv_Count * 8) & 0xff);
|
|
|
750
|
- } /** end of for (int liv_Count = 0; liv_Count < length; liv_Count++) */
|
|
|
751
|
-
|
|
|
752
|
- return mByte;
|
|
|
753
|
- }
|
|
|
754
|
-
|
|
|
755
|
- /**
|
|
|
756
|
- * 字节数据转十进制字符(补码-无符号)
|
|
|
757
|
- *
|
|
|
758
|
- * @param data
|
|
|
759
|
- * @return
|
|
|
760
|
- */
|
|
|
761
|
- public static String getByteToStr(byte[] data) {
|
|
|
762
|
- StringBuffer str = new StringBuffer();
|
|
|
763
|
- for (int i = 0; i < data.length; i++) {
|
|
|
764
|
- if (data[i] < 0) {
|
|
|
765
|
- int tem = data[i] + 256;
|
|
|
766
|
- str.append(tem + " ");
|
|
|
767
|
- } else {
|
|
|
768
|
- str.append(data[i] + " ");
|
|
|
769
|
- }
|
|
|
770
|
- }
|
|
|
771
|
- return str.toString();
|
|
|
772
|
- }
|
|
|
773
|
-
|
|
|
774
|
- public static byte[] buf2Bytes(ByteBuf in) {
|
|
|
775
|
- return buf2Bytes( in, in.readableBytes()) ;
|
|
|
776
|
- }
|
|
|
777
|
- public static byte[] buf2Bytes(ByteBuf in,int readable) {
|
|
|
778
|
- byte[] dst = new byte[readable];
|
|
|
779
|
- in.getBytes(0, dst);
|
|
|
780
|
- return dst;
|
|
|
781
|
- }
|
|
|
782
|
-
|
|
|
783
|
- public static byte[] fileBytes(String pathname) throws IOException {
|
|
|
784
|
- File filename = new File(pathname);
|
|
|
785
|
- BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
|
|
|
786
|
- ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
|
|
|
787
|
- byte[] temp = new byte[1024];
|
|
|
788
|
- int size = 0;
|
|
|
789
|
- while((size = in.read(temp)) != -1){
|
|
|
790
|
- out.write(temp, 0, size);
|
|
|
791
|
- }
|
|
|
792
|
- in.close();
|
|
|
793
|
- out.close();
|
|
|
794
|
- byte[] content = out.toByteArray();
|
|
|
795
|
- return content;
|
|
|
796
|
- }
|
|
|
797
|
- /**
|
|
|
798
|
- * 单字节高低位互换
|
|
|
799
|
- * @param src
|
|
|
800
|
- * @return
|
|
|
801
|
- */
|
|
|
802
|
- public static int revert(int src) {
|
|
|
803
|
- int lowByte = (src & 0xFF00) >> 8;
|
|
|
804
|
- int highByte = (src & 0x00FF) << 8;
|
|
|
805
|
- return lowByte | highByte;
|
|
|
806
|
- }
|
|
|
807
|
-
|
|
|
808
|
- /**
|
|
|
809
|
- * 将字符串编码成16进制数字,适用于所有字符(包括中文)
|
|
|
810
|
- * @param str 字符串
|
|
|
811
|
- * @return
|
|
|
812
|
- */
|
|
|
813
|
- public static String stringEncodeToHex(String str) {
|
|
|
814
|
- //根据默认编码获取字节数组
|
|
|
815
|
- byte[] bytes=str.getBytes();
|
|
|
816
|
- StringBuilder sb=new StringBuilder(bytes.length*2);
|
|
|
817
|
- //将字节数组中每个字节拆解成2位16进制整数
|
|
|
818
|
- for(int i=0;i<bytes.length;i++)
|
|
|
819
|
- {
|
|
|
820
|
- sb.append(hexString.charAt((bytes[i]&0xf0)>>4));
|
|
|
821
|
- sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
|
|
|
822
|
- }
|
|
|
823
|
- return sb.toString();
|
|
|
824
|
-
|
|
|
825
|
- }
|
|
|
826
|
-
|
|
|
827
|
- /**
|
|
|
828
|
- * 将16进制数字解码成字符串,适用于所有字符(包括中文)
|
|
|
829
|
- * @param hex 16进制
|
|
|
830
|
- * @return
|
|
|
831
|
- */
|
|
|
832
|
- public static String hexDecodeToString(String hex) {
|
|
|
833
|
- try{
|
|
|
834
|
- byte[] baKeyword = new byte[hex.length()/2];
|
|
|
835
|
- for(int i = 0; i < baKeyword.length; i++) {
|
|
|
836
|
- baKeyword[i] = (byte)(0xff & Integer.parseInt(hex.substring(i*2, i*2+2),16));
|
|
|
837
|
- }
|
|
|
838
|
- return new String(baKeyword,"UTF-8");
|
|
|
839
|
- }catch (UnsupportedEncodingException e){
|
|
|
840
|
- return null;
|
|
|
841
|
- }
|
|
|
842
|
- }
|
|
|
843
|
-
|
|
|
844
|
- public static byte[] hexToByteArray(String inHex){
|
|
|
845
|
- inHex.replace(" ","");
|
|
|
846
|
- int hexlen = inHex.length();
|
|
|
847
|
- byte[] result;
|
|
|
848
|
- if (hexlen % 2 == 1){
|
|
|
849
|
- //奇数
|
|
|
850
|
- hexlen++;
|
|
|
851
|
- result = new byte[(hexlen/2)];
|
|
|
852
|
- inHex="0"+inHex;
|
|
|
853
|
- }else {
|
|
|
854
|
- //偶数
|
|
|
855
|
- result = new byte[(hexlen/2)];
|
|
|
856
|
- }
|
|
|
857
|
- int j=0;
|
|
|
858
|
- for (int i = 0; i < hexlen; i+=2){
|
|
|
859
|
- result[j]=hexToByte(inHex.substring(i,i+2));
|
|
|
860
|
- j++;
|
|
|
861
|
- }
|
|
|
862
|
- return result;
|
|
|
863
|
- }
|
|
|
864
|
- public static byte hexToByte(String inHex){
|
|
|
865
|
- return (byte)Integer.parseInt(inHex,16);
|
|
|
866
|
- }
|
|
|
867
|
-} |
|
|