自动化立体仓库 - WCS系统
#
luxiaotao1123
2020-08-05 7e36eb78c63d2ed483bb8f717666f25c54f2e8b3
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package com.zy.common.HslCommunication;
 
 
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
 
 
/**
 * 一个工具类,为了支持和C#程序通信交互的转换工具类
 */
public class Utilities {
 
    /**
     * 将short数据类型转化成Byte数组
     * @param data short值
     * @return byte[]数组
     */
    public static byte[] getBytes(short data) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (data & 0xff);
        bytes[1] = (byte) ((data & 0xff00) >> 8);
        return bytes;
    }
 
    /**
     * 将int数据类型转化成Byte数组
     * @param data int值
     * @return byte[]数组
     */
    public static byte[] getBytes(int data) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (data & 0xff);
        bytes[1] = (byte) ((data >> 8) & 0xff);
        bytes[2] = (byte) ((data >> 16) & 0xff);
        bytes[3] = (byte) ((data >> 24) & 0xff);
        return bytes;
    }
 
    /**
     * 将long数据类型转化成Byte数组
     * @param data long值
     * @return byte[]数组
     */
    public static byte[] getBytes(long data) {
        byte[] bytes = new byte[8];
        bytes[0] = (byte) (data & 0xff);
        bytes[1] = (byte) ((data >> 8) & 0xff);
        bytes[2] = (byte) ((data >> 16) & 0xff);
        bytes[3] = (byte) ((data >> 24) & 0xff);
        bytes[4] = (byte) ((data >> 32) & 0xff);
        bytes[5] = (byte) ((data >> 40) & 0xff);
        bytes[6] = (byte) ((data >> 48) & 0xff);
        bytes[7] = (byte) ((data >> 56) & 0xff);
        return bytes;
    }
 
    /**
     * 将float数据类型转化成Byte数组
     * @param data float值
     * @return byte[]数组
     */
    public static byte[] getBytes(float data) {
        int intBits = Float.floatToIntBits(data);
        return getBytes(intBits);
    }
 
    /**
     * 将double数据类型转化成Byte数组
     * @param data double值
     * @return byte[]数组
     */
    public static byte[] getBytes(double data) {
        long intBits = Double.doubleToLongBits(data);
        return getBytes(intBits);
    }
 
 
    /**
     * 将字符串转换成byte[]数组
     * @param data 字符串值
     * @param charsetName 编码方式
     * @return 字节数组
     */
    public static byte[] getBytes(String data, String charsetName) {
        Charset charset = Charset.forName(charsetName);
        return data.getBytes(charset);
    }
 
 
    /**
     * 将int转换成一个字节的数据
     * @param num int数据
     * @return 一个字节的数据
     */
    public static byte int2OneByte(int num) {
        return (byte) (num & 0x000000ff);
    }
 
 
    /**
     * 将一个有符号的byte转换成一个int数据对象
     * @param byteNum 有符号的字节对象
     * @return int数据类型
     */
    public static int oneByte2Int(byte byteNum) {
        //针对正数的int
        return byteNum > 0 ? byteNum : 256+ byteNum;
    }
 
 
    /**
     * 将字节数组转换成short数据
     * @param bytes 字节数组
     * @param index 起始位置
     * @return short值
     */
    public static short getShort(byte[] bytes,int index) {
        return (short) ((0xff & bytes[0 + index]) | (0xff00 & (bytes[1 + index] << 8)));
    }
 
 
    /**
     * 将字节数组转换成int数据
     * @param bytes 字节数组
     * @param index 起始位置
     * @return int值
     */
    public static int getInt(byte[] bytes,int index) {
        return (0xff & bytes[0 + index]) |
                (0xff00 & (bytes[1 + index] << 8)) |
                (0xff0000 & (bytes[2 + index] << 16)) |
                (0xff000000 & (bytes[3 + index] << 24));
    }
 
 
    /**
     * 将字节数组转换成long数据
     * @param bytes 字节数组
     * @param index 起始位置
     * @return long值
     */
    public static long getLong(byte[] bytes,int index) {
        return (0xffL & (long) bytes[0 + index]) |
                (0xff00L & ((long) bytes[1 + index] << 8)) |
                (0xff0000L & ((long) bytes[2 + index] << 16)) |
                (0xff000000L & ((long) bytes[3 + index] << 24)) |
                (0xff00000000L & ((long) bytes[4 + index] << 32)) |
                (0xff0000000000L & ((long) bytes[5 + index] << 40)) |
                (0xff000000000000L & ((long) bytes[6 + index] << 48)) |
                (0xff00000000000000L & ((long) bytes[7 + index] << 56));
    }
 
 
    /**
     * 将字节数组转换成float数据
     * @param bytes 字节数组
     * @param index 起始位置
     * @return float值
     */
    public static float getFloat(byte[] bytes,int index) {
        return Float.intBitsToFloat(getInt(bytes,index));
    }
 
 
    /**
     * 将字节数组转换成double数据
     * @param bytes 字节数组
     * @param index 起始位置
     * @return double值
     */
    public static double getDouble(byte[] bytes,int index) {
        long l = getLong(bytes, index);
        System.out.println(l);
        return Double.longBitsToDouble(l);
    }
 
    /**
     * 将字节数组转换成string数据
     * @param bytes 字节数组
     * @param charsetName 字符编码
     * @return string值
     */
    public static String getString(byte[] bytes, String charsetName) {
        return new String(bytes, Charset.forName(charsetName));
    }
 
 
    /**
     * 将字节数组转换成string数据
     * @param bytes 字节数组
     * @param index 起始位置
     * @param length 数据长度
     * @param charsetName 字符编码
     * @return string值
     */
    public static String getString(byte[] bytes,int index, int length, String charsetName) {
        return new String(bytes,index,length,Charset.forName(charsetName));
    }
 
 
    /**
     * 将一个byte[]数组转换成uuid对象
     * @param data 字节数组
     * @return uuid对象
     */
    public static UUID Byte2UUID(byte[] data) {
        if (data.length != 16) {
            throw new IllegalArgumentException("Invalid UUID byte[]");
        }
        long msb = 0;
        long lsb = 0;
        for (int i = 0; i < 8; i++)
            msb = (msb << 8) | (data[i] & 0xff);
        for (int i = 8; i < 16; i++)
            lsb = (lsb << 8) | (data[i] & 0xff);
 
        return new UUID(msb, lsb);
    }
 
 
    /**
     * 将一个uuid对象转化成byte[]
     * @param uuid uuid对象
     * @return 字节数组
     */
    public static byte[] UUID2Byte(UUID uuid) {
        ByteArrayOutputStream ba = new ByteArrayOutputStream(16);
        DataOutputStream da = new DataOutputStream(ba);
        try {
            da.writeLong(uuid.getMostSignificantBits());
            da.writeLong(uuid.getLeastSignificantBits());
            ba.close();
            da.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
 
        byte[] buffer = ba.toByteArray();
        // 进行错位
        byte temp=buffer[0];
        buffer[0] = buffer[3];
        buffer[3] =temp;
        temp=buffer[1];
        buffer[1]=buffer[2];
        buffer[2]=temp;
 
        temp = buffer[4];
        buffer[4]=buffer[5];
        buffer[5] =temp;
 
        temp = buffer[6];
        buffer[6]=buffer[7];
        buffer[7] =temp;
 
        return buffer;
    }
 
 
 
    /**
     * 将字符串数据转换成字节数组,主要转换由C#的字符串的数据
     * @param str 字符串信息
     * @return 转化后的字节数组
     */
    public static byte[] string2Byte(String str) {
        if (str == null) {
            return null;
        }
        byte[] byteArray;
        try {
            byteArray = str.getBytes("unicode");
        } catch (Exception ex) {
            byteArray = str.getBytes();
        }
 
        if (byteArray.length >= 2) {
            if (byteArray[0] == -1 && byteArray[1] == -2) {
                byte[] newArray = new byte[byteArray.length - 2];
                System.arraycopy(byteArray, 2, newArray, 0, newArray.length);
                byteArray = newArray;
            } else if (byteArray[0] == -2 && byteArray[1] == -1) {
                for (int i = 0; i < byteArray.length; i++) {
                    byte temp = byteArray[i];
                    byteArray[i] = byteArray[i + 1];
                    byteArray[i + 1] = temp;
                    i++;
                }
 
 
                byte[] newArray = new byte[byteArray.length - 2];
                System.arraycopy(byteArray, 2, newArray, 0, newArray.length);
                byteArray = newArray;
            }
        }
 
        return byteArray;
    }
 
 
    /**
     * 将字节数组转换成字符串对象,主要转换由C#的字符串的数据
     * @param byteArray
     * @return
     */
    public static String byte2String(byte[] byteArray) {
        if (byteArray == null) {
            return null;
        }
 
        for (int i = 0; i < byteArray.length; i++) {
            byte temp = byteArray[i];
            byteArray[i] = byteArray[i + 1];
            byteArray[i + 1] = temp;
            i++;
        }
        String str;
        try {
            str = new String(byteArray, "unicode");
        } catch (Exception ex) {
            str = new String(byteArray);
        }
        return str;
    }
 
    /**
     * 将byte[]数组的数据进行翻转
     * @param reverse 等待反转的字符串
     */
    public static void bytesReverse(byte[] reverse) {
        if (reverse != null) {
            byte tmp = 0;
            for (int i = 0; i < reverse.length / 2; i++) {
                tmp = reverse[i];
                reverse[i] = reverse[reverse.length - 1 - i];
                reverse[reverse.length - 1 - i] = tmp;
            }
        }
    }
 
 
 
    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 
 
    /**
     * 将字节数组转换成十六进制的字符串形式
     * @param bytes 原始的字节数组
     * @return 字符串信息
     */
    public static String bytes2HexString(byte[] bytes) {
        char[] buf = new char[bytes.length * 2];
        int index = 0;
        for(byte b : bytes) { // 利用位运算进行转换,可以看作方法一的变种
            buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
            buf[index++] = HEX_CHAR[b & 0xf];
        }
 
        return new String(buf);
    }
 
    /**
     * 获取指定时间的指定格式的字符串
     * @param date 指定的时间
     * @param format 指定的格式
     * @return 最后字符串信息
     */
    public static String getStringDateShort(Date date,String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(date);
        return dateString;
    }
}