严智鑫
2024-09-05 7da8422f45751fffd5666a0260196a3d9605b7dc
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
package com.mes.utils;
 
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class HexUtil {
    
    public static String formatHex(String hex) {
        String result = "";
        
        for (int i = 0; i < hex.length() - 1; i+=2) {
            String output = hex.substring(i, i + 2);
            result += ("0x" + output + " ");
        }
        
        if (result.length() > 0) {
            result = result.substring(0, result.lastIndexOf(" "));
        }
        
        return result;
    }
    
    //正常的字母数字变成字节数发送给盒子
    public static String asciiToHex(String str) {
        char[] chars = str.toCharArray();
        
        StringBuffer hex = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            hex.append(Integer.toHexString(chars[i]));
        }
        
        return hex.toString();
    }
    
    public static String hexToAscii(String hex) {
        StringBuffer result = new StringBuffer();
        
        for (int i = 0; i < hex.length() - 1; i+= 2) {
            String output = hex.substring(i, i + 2);
            int decimal = Integer.parseInt(output, 16);
            result.append((char) decimal);
        }
        
        return result.toString();
    }//12300    //00321
    //二进制转十进制
    public static int int2ToHex(String number) {
        return Integer.parseInt(number, 2);
    }
    //二进制转16进制 4位
    public static String intBinaryTo16(String number) {
        int num=int2ToHex(number);
        return intTo2ByteHex(num);
    }
    //十六进制转十进制
    public static int int16ToHex(String number) {
        return Integer.parseInt(number, 16);
    }
    //十进制转二进制
    public static String intToBinary(int number) {
        return Integer.toBinaryString(number);
    }
    //十六进制转二进制 
    public static String int16ToBinary(String number) {
        return intToBinary(int16ToHex(number));
    }
    //十六进制转二进制  
    public static String int16ToBinaryEight(String number,int count) {
        String binary=int16ToBinary(number);
        String zero="";
        for (int i = 0; i <count-binary.length(); i++) {
            zero+="0";
        }
        binary=zero+binary;
        return binary;
    }
    
    public static String intToHex(int number) {
        return Integer.toHexString(number);
    }
    
    /**
     * 将整数转换为2位16进制,如1转换为01,10转换为0a
     * 
     * @param number
     * @return
     */
    public static String intTo1ByteHex(int number) {
        String numberHex = HexUtil.intToHex(number);
        
        numberHex = String.format("%2s", numberHex).replace(' ', '0');
        
        return numberHex;
    }
    
    /**
     * 将整数转换为4位16进制,如1转换为0001,10转换为000a
     * 
     * @param number
     * @return
     */
    public static String intTo2ByteHex(int number) {
        String numberHex = HexUtil.intToHex(number);
        
        numberHex = String.format("%4s", numberHex).replace(' ', '0');
        
        return numberHex;
    }
    
    /**
     * 将整数转换为8位16进制,如1转换为00000001,10转换为0000000a
     * 
     * @param number
     * @return
     */
    public static String intTo4ByteHex(int number) {
        String numberHex = HexUtil.intToHex(number);       
        numberHex = String.format("%8s", numberHex).replace(' ', '0');       
        return numberHex;
    }
    
    /**
     * 将时间转换为16进制格式,年(2字节) + 月(1字节) + 日(1字节) + 时(1字节) + 分(1字节) + 秒(1字节)
     * 
     * @param time
     * @return
     */
    public static String timeToHex(Date time) {
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        
        String yearHex = intTo2ByteHex(c.get(Calendar.YEAR));
        String monthHex = intTo1ByteHex(c.get(Calendar.MONTH) + 1);
        String dayHex = intTo1ByteHex(c.get(Calendar.DAY_OF_MONTH));
        String hourHex = intTo1ByteHex(c.get(Calendar.HOUR_OF_DAY));
        String minuteHex = intTo1ByteHex(c.get(Calendar.MINUTE));
        String secondHex = intTo1ByteHex(c.get(Calendar.SECOND));
        
        return yearHex + monthHex + dayHex + hourHex + minuteHex + secondHex;
    }
    
    /**
     * 将16进制的时间转换为yyyy-MM-dd HH:mm:ss的格式
     * 
     * @param hexTime
     * @return
     */
    public static String hexToTime(String hexTime) {
        String year = hexTo4DigitInt(hexTime.substring(0, 4));
        String month = hexTo2DigitInt(hexTime.substring(4, 6));
        String day = hexTo2DigitInt(hexTime.substring(6, 8));
        String hour = hexTo2DigitInt(hexTime.substring(8, 10));
        String minute = hexTo2DigitInt(hexTime.substring(10, 12));
        String second = hexTo2DigitInt(hexTime.substring(12, 14));
        
        return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    }
    //十六进制转10进制
    public static int hexToInt(String hex) {
        return Integer.parseInt(hex, 16);
    }
    //十六进制转成10进制四位
    public static String hexTo4DigitInt(String hex) {
        return new DecimalFormat("0000").format(hexToInt(hex));
    }
    //十六进制转成10进制两位
    public static String hexTo2DigitInt(String hex) {
        return new DecimalFormat("00").format(hexToInt(hex));
    }
    
    public static byte[] stringToInt(String a){
        byte[] byt = new byte[a.length()/2];
        for (int i = 0; i < a.length() - 1; i+=2) {
            String output = a.substring(i, i + 2);
            byt[i/2]=(byte)Integer.parseInt(output, 16);
        }
 
        return byt;
    }
    
    /**
     * 将字节转换为两位十六进制字符串,不够位前加0
     * 
     * @param b
     * @return
     */
    public static String byteToHexString(byte b) {
        String hex = Integer.toHexString(b & 0xFF);
        
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        
        return hex;
    }
    
    /**
     * 将十六进制字符,转换成二进制的字符
     * 
     * @param b
     * @return
     */
     public static String getBin(String hex){
        String[] hexs = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                 "a", "b", "c", "d", "e", "f"};
        String[] bins = new String[]{"0000", "0001", "0010", "0011", "0100", "0101",
                 "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
        int i;
        for(i=0; i<hexs.length && !hex.toLowerCase().equals(hexs[i]); i++);
            return bins[i];
         }
    
       /**
         * 将十六进制字符,转换成二进制的字符
         * 
         * @param b
         * @return
         */
         public static String gethex(String hex){
            String[] hexs = new String[]{"0000", "0001", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009",
                     "000a", "000b", "000c", "000d", "000e", "000f"};
            String[] bins = new String[]{"0000", "0001", "0010", "0011", "0100", "0101",
                     "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
            int i;
            for(i=0; i<bins.length && !hex.toLowerCase().equals(bins[i]); i++);
                return hexs[i];
             }
     
     
    
    /**
     * 十进制字节数组转十六进制字符串
     * @param bufSize
     * @param msg
     * @return
     */
    public static String byteToHexString(int bufSize,byte[] msg){
        String tempHex = "";
        String command = "";
        if (bufSize != -1) {
            for (int i = 0; i < bufSize; i++) {
                tempHex = Integer.toHexString(msg[i] & 0xFF);
 
                if (tempHex.length() == 1) {
                    tempHex = "0" + tempHex;
                }
                command += tempHex;
            }
        }
        return command;
    }
    
    public static String hexToBinary(String hexString) {
        String binaryString = Integer.toBinaryString(hexToInt(hexString));
        return String.format("%16s", binaryString).replace(' ', '0');
    }
    
    public static String binaryTo2ByteHex(String binaryString) {
        String HexString = Integer.toHexString(Integer.parseInt(binaryString, 2));
        return String.format("%4s", HexString).replace(' ', '0');
    }
    
    public static void main(String[] args) {
//        System.out.println(HexUtil.hexToAscii("3c5354413e48656c6c6f20576f726c64217c5468697320697320746865206669727374207369676e616c2066726f6d20646576696365213c454f463e"));
//        System.out.println(HexUtil.asciiToHex("<STA>Hello World!|This is the first signal from device!<EOF>"));
//        
//        System.out.println(String.format("%4S", HexUtil.intToHex(55)).replace(' ', '0'));
//        System.out.println(HexUtil.hexToInt("00d2"));
//        
//        System.out.println(HexUtil.formatHex("3c5354413e"));
        String message = "Hello World!|This is the first signal from device!";
        int length = message.length() * 2 + 10; // 长度包括结尾的<EOF>,一个字符在信号中由两个字节表示。
        
        String command = HexUtil.asciiToHex("<STA>"); // 添加开始标识
        
        command += (String.format("%4s", HexUtil.intToHex(length)).replace(' ', '0')); // 添加长度标识
        command += "01"; // 添加设备类型 0x01表示切割机,0x02表示钢化炉
        command += "0001"; // 添加设备型号,0x0001表示基本款,0x0002表示中级款
        command += "0000"; // 功能号,0x0000是下位机主动发给上位机;0x0001是上位机修改下位机RTC
        command += "00"; // 加密方式,0x00表示不加密
        
        Calendar c = Calendar.getInstance();
        
        // 添加时间,并将时间转换为16进制表示
        command += (String.format("%4s", HexUtil.intToHex(c.get(Calendar.YEAR))).replace(' ', '0'));
        command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.MONTH) + 1)).replace(' ', '0'));
        command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.DAY_OF_MONTH))).replace(' ', '0'));
        command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.HOUR_OF_DAY))).replace(' ', '0'));
        command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.MINUTE))).replace(' ', '0'));
        command += (String.format("%2s", HexUtil.intToHex(c.get(Calendar.SECOND))).replace(' ', '0'));
        
        command += ("00000001"); // 唯一标识,使用序列号
        
        command += (HexUtil.asciiToHex(message)); // 添加主要信息内容
        
        command += (HexUtil.asciiToHex("<EOF>")); // 添加结尾标识
        
        System.out.println(command);
        
        System.out.println(String.format("%4s", "1"));
        
        Date time = new Date();
        System.out.println(new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(time));
        
        String timeHex = timeToHex(time);
        System.out.println(timeHex);
        System.out.println(hexToTime(timeHex));
        
        System.out.println("3c5354413e001a00000000000000000000000000010007e0021500152800000000000000005041001d017c017c01017c3c454f463e".length());
        
        System.out.println(hexToBinary("0c0a"));
        
        System.out.println(Integer.MAX_VALUE);
    }
    
    public static String hexTo2Binary(String hexString) {
        String binaryString = Integer.toBinaryString(hexToInt(hexString));
        return String.format("%8s", binaryString).replace(' ', '0');
    }
}