liurunyu
2024-08-15 f7e731bdc2fce4445c0d22993134c6c2b07d207b
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
package com.ruoyi.common.utils.netty;
 
 
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
 
public class NettyTool {
 
    /**
     * 每找到一个“ABCD”,就将字符串截取,直到找到第三个,将每次下标累加即可。
     *
     * @param str1
     * @param str2
     * @return
     */
    public static Integer getIndex(String str1, String str2) {
        int num = 0;
        int location = 0;
 
        int index = str1.indexOf(str2);
        while (-1 != str1.indexOf(str2)) {
            location += index;
 
            num++;
            if (3 == num) {
                break;
            } else {
                str1 = str1.substring(index);
                index = str1.indexOf(str2, 1);
            }
        }
 
        return location;
    }
 
    public static List<Integer> getIndexs(String str1, String str2) {
 
        int length = str2.length();
 
        int index;
        int location = 0;
        List<Integer> indexs = new ArrayList<>();
        while (-1 != str1.indexOf(str2)) {
            index = str1.indexOf(str2);
 
            indexs.add(location + index);
 
            index = index + length;
 
            location = location + index;
 
            str1 = str1.substring(index);
 
        }
        return indexs;
    }
 
 
 
 
 
    public static String readCityFile(MultipartFile multipartFile) {
        StringBuilder stringBuilder = null;
        try {
            if (multipartFile!=null) {
                InputStream bb = multipartFile.getInputStream();
                InputStreamReader streamReader = new InputStreamReader(bb);
                BufferedReader reader = new BufferedReader(streamReader);
                String line;
                stringBuilder = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                reader.close();
                bb.close();
            } else {
                stringBuilder.append("空的");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return String.valueOf(stringBuilder);
    }
 
    //对找map下表
    public static int Selectsubscript(Object[] keys, String xiabiao) {
        //遍历key 找到
        Integer mapkey1 = Integer.parseInt(xiabiao,16);
        String mapkey="@"+Integer.toHexString(mapkey1).toUpperCase();
        int a = 0;
        for (int i = 0; i < ChannelHandlerContextCache.keys.length; i++) {
            String data1=String.valueOf(ChannelHandlerContextCache.keys[i]);
            if (data1.equals( mapkey)) {
                a = i;
            }
        }
        return a;
    }
 
    public static int Calculatepagenumber(Map<String, List<String>> cachepage) {
        int a = 0;
        for (List<String> value : cachepage.values()) {
            int a1 = value.size();
            a = a + a1;
        }
        return a;
    }
 
    //计算块数量
    public static Integer Numberofblocks(Map<String, List<String>> cachepage) {
        int a = 0;
        for (String key : cachepage.keySet()) {
            a = a + 1;
        }
 
        return a;
    }
 
    //向右补位
    public static String addstrfFForNum(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                //sb.append("f").append(str);// 左补0
                sb.append(str).append("f");//右补0
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }
 
 
 
    //向左补位
    public static String addleftForNum(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(str);// 左补0
//                sb.append(str).append("f");//右补0
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }
 
 
    /**
     * 把原始字符串分割成指定长度的字符串列表
     *
     * @param inputString 原始字符串
     * @param length      指定长度
     * @return
     */
    public static List<String> getStrList(String inputString, int length) {
        int size = inputString.length() / length;
        if (inputString.length() % length != 0) {
            size += 1;
        }
        return getStrList(inputString, length, size);
    }
 
    /**
     * 把原始字符串分割成指定长度的字符串列表
     *
     * @param inputString 原始字符串
     * @param length      指定长度
     * @param size        指定列表大小
     * @return
     */
    public static List<String> getStrList(String inputString, int length,
                                          int size) {
        List<String> list = new ArrayList<String>();
        for (int index = 0; index < size; index++) {
            String childStr = substring(inputString, index * length,
                    (index + 1) * length);
            list.add(childStr);
        }
        return list;
    }
 
 
    /**
     * 分割字符串,如果开始位置大于字符串长度,返回空
     *
     * @param str 原始字符串
     * @param f   开始位置
     * @param t   结束位置
     * @return
     */
    public static String substring(String str, int f, int t) {
        if (f > str.length()){
            return null;
        }
 
        if (t > str.length()) {
            return str.substring(f, str.length());
        } else {
            return str.substring(f, t);
        }
    }
 
 
 
    /*开始编写 将map开始拆分*/
    public static Map<String, List<String>> cachepage(Map<String, String> filemap) {
        //开始遍历 map
        Map<String, List<String>> cachepage = new LinkedHashMap<>();
        List<String> lists = null;
        for (String key : filemap.keySet()) {
            System.out.println("Key = " + key);
            //根据kay 获取到map
            String a = filemap.get(key);
            if (a.length() > 1024) {
                //如果大于则开始分段
                lists = getStrList(a, 1024);
                cachepage.put(key, lists);
            } else {
                cachepage.put(key, Arrays.asList(a.split(",")));
            }
        }
        return cachepage;
    }
 
    /**
     * 读取File文件的信息内容
     *
     * @param file
     * @return
     */
    public static String txt2String(File file) {
        String result = "";
        try {
            //构造一个BufferedReader类来读取文件
            BufferedReader br = new BufferedReader(new FileReader(file));
            String s = null;
            //使用readLine方法,一次读一行
            while ((s = br.readLine()) != null) {
                result = result + "\n" + s;
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    public static Map<String, String> xiabiao(String str) {
        //第一种方法
        String st1 = str;
        int frontLength = 0;//定义该变量用于记录匹配"love"的元素前面的长度
        ArrayList<Integer> lists = new ArrayList();
        while (str.contains("@")) {//只要该str字符串中有匹配"love"的元素,才进行以下操作
            int index = str.indexOf("@");//定义该变量用于记录匹配"love"的元素在当前字符串的位置
            //匹配"love"的元素位置等于frontLength加上index;加1为了从1开始计数,更加直观:
            System.out.println(String.format("str[%d] = @", index + frontLength + 1));
            lists.add(index + frontLength);
            frontLength += (index + 5);
            str = str.substring(index + 5);//将字符串中匹配"love"元素的前面部分及其本身截取,留下后面的部分
        }
        String value = "";
        String key = "";
        Map<String, String> fileMpa = new LinkedHashMap<>();
        //开始对 list 遍历并且添加到 map 中去
        int ss = st1.length();
        for (int i = 0; i < lists.size(); i++) {
            if (i == lists.size() - 1) {
                value = st1.substring(lists.get(i) + 5, st1.length() - 1);
                key = st1.substring(lists.get(i), lists.get(i) + 5);
 
 
            } else {
                key = st1.substring(lists.get(i), lists.get(i) + 5);
                value = st1.substring(lists.get(i) + 5, lists.get(i + 1));
            }
            fileMpa.put(key, value);
        }
        return fileMpa;
    }
 
 
    public static Map<String, String> xiabiao_new (String str, List<String> kuai1List) {
        Map<String, String> fileMpa = new LinkedHashMap<>();
        for (int i = 0; i < kuai1List.size(); i++) {
            String key;
            String value;
 
            if (i == kuai1List.size() - 1) {
                key =  kuai1List.get(i);
                value = str.substring(str.indexOf(kuai1List.get(i))+kuai1List.get(i).length(), str.length()-1);
            } else {
                key = kuai1List.get(i);
                value = str.substring(str.indexOf(kuai1List.get(i))+kuai1List.get(i).length(), str.indexOf(kuai1List.get(i+1)));
            }
            fileMpa.put(key, value);
        }
        return fileMpa;
 
    }
 
 
    //获取主机的当前的时间
    public static String getDateTime() {
        Calendar cal = Calendar.getInstance();
        int y = cal.get(Calendar.YEAR);
        String year = Integer.toString(y - 2000);
        int m = cal.get(Calendar.MONTH) + 1;
        String month = String.format("%02d", m);
        int d = cal.get(Calendar.DATE);
        String date = String.format("%02d", d);
        int h = cal.get(Calendar.HOUR_OF_DAY);
        String HOUR = String.format("%02d", h);
        int mi = cal.get(Calendar.MINUTE);
        String MINUTE = String.format("%02d", mi);
        int s = cal.get(Calendar.SECOND);
        String SECOND = String.format("%02d", s);
        String data = year + month + date + HOUR + MINUTE + SECOND;
        return data;
    }
 
 
    //计算CRC16校验码 //低位在前 高位在后
    public static String getCRC(String data) {
        data = data.replace(" ", "");
        int len = data.length();
        if (!(len % 2 == 0)) {
            return "0000";
        }
        int num = len / 2;
        byte[] para = new byte[num];
        for (int i = 0; i < num; i++) {
            int value = Integer.valueOf(data.substring(i * 2, 2 * (i + 1)), 16);
            para[i] = (byte) value;
        }
        return getCRC(para);
    }
 
    /**
     * 计算CRC16校验码
     *
     * @param bytes 字节数组
     * @return {@link String} 校验码
     * @since 1.0
     */
    public static String getCRC(byte[] bytes) {
        // CRC寄存器全为1
        int CRC = 0x0000ffff;
        // 多项式校验值
        int POLYNOMIAL = 0x0000a001;
        int i, j;
        for (i = 0; i < bytes.length; i++) {
            CRC ^= ((int) bytes[i] & 0x000000ff);
            for (j = 0; j < 8; j++) {
                if ((CRC & 0x00000001) != 0) {
                    CRC >>= 1;
                    CRC ^= POLYNOMIAL;
                } else {
                    CRC >>= 1;
                }
            }
        }
 
        // 结果转换为16进制
        String result = Integer.toHexString(CRC).toUpperCase();
        if (result.length() != 4) {
            StringBuffer sb = new StringBuffer("0000");
            result = sb.replace(4 - result.length(), 4, result).toString();
        }
        //低位在前 高位在后
        return result.substring(2, 4) + result.substring(0, 2);
        // 高位为在前 低位在后
        // return   result.substring(0, 2)+  result.substring(2, 4)
        // return   (result.substring(0, 2)+  result.substring(2, 4)).toLowerCase();
    }
 
 
    // 高位为在前 低位在后
 
    public static String getCRChigh(String data) {
        data = data.replace(" ", "");
        int len = data.length();
        if (!(len % 2 == 0)) {
            return "0000";
        }
        int num = len / 2;
        byte[] para = new byte[num];
        for (int i = 0; i < num; i++) {
            int value = Integer.valueOf(data.substring(i * 2, 2 * (i + 1)), 16);
            para[i] = (byte) value;
        }
        return getCRChigh(para);
    }
 
    /**
     * 计算CRC16校验码
     *
     * @param bytes 字节数组
     * @return {@link String} 校验码
     * @since 1.0
     */
    public static String getCRChigh(byte[] bytes) {
        // CRC寄存器全为1
        int CRC = 0x0000ffff;
        // 多项式校验值
        int POLYNOMIAL = 0x0000a001;
        int i, j;
        for (i = 0; i < bytes.length; i++) {
            CRC ^= ((int) bytes[i] & 0x000000ff);
            for (j = 0; j < 8; j++) {
                if ((CRC & 0x00000001) != 0) {
                    CRC >>= 1;
                    CRC ^= POLYNOMIAL;
                } else {
                    CRC >>= 1;
                }
            }
        }
 
        // 结果转换为16进制
        String result = Integer.toHexString(CRC).toUpperCase();
        if (result.length() != 4) {
            StringBuffer sb = new StringBuffer("0000");
            result = sb.replace(4 - result.length(), 4, result).toString();
        }
        //低位在前 高位在后
        // return result.substring(2, 4) +  result.substring(0, 2);
        // 高位为在前 低位在后
        return result.substring(0, 2) + result.substring(2, 4);
        // return   (result.substring(0, 2)+  result.substring(2, 4)).toLowerCase();
    }
 
 
    //获取字节的长度 并将其修改成16位数
    public static String Hexlength(String Hexstr) {
 
        int a = Hexstr.length() / 2;
        String lenght = ConvertCode.intToHexString(a, 1);
 
        return lenght;
    }
 
 
    //对数据 进行截取
    public static String InterceptController(String requestData) {
        return requestData.substring(20, 22);
    }
 
 
    //控制器
    public static String getController(String data) {
 
        return data.substring(6, 16);
    }
 
 
    //密码
    public static String getPassword(String requestData) {
 
        String data = requestData.substring(16, 20);
        return data;
    }
 
    //固定
    public static String getfixed() {
        String data = "2F";
        return data;
    }
 
    /**
     * 时间日期转换
     *
     * @param strDate 字符串yyyyMMddHHmmss
     * @return 字符串yyyy-MM-dd HH:mm:ss  2019 06 19 12 00
     * 注意 提前向字符前补位“20”
     */
    public static String strToDateLong(String strDate) {
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyyMMddHHmmss").parse("20" + strDate);//先按照原格式转换为时间
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);//再将时间转换为对应格式字符串
        return str;
    }
 
    /**
     * 将字符串的长度为0,直接将其数值修改为0
     *
     * @param number
     * @return String
     */
 
    public static String returnToZero(String number) {
        if (number.length() == 0) {
            return "0";
        } else {
            return number;
        }
    }
 
 
    public static String replace(String str) {
        String destination = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            destination = m.replaceAll("");
        }
        return destination;
    }
 
 
    //根据value 找到key
    public static <K, V> K getKeyByLoop(Map<K, V> map, V value) {
        for (Map.Entry<K, V> entry : map.entrySet()) {
            if (Objects.equals(entry.getValue(), value)) {
                return entry.getKey();
            }
        }
        return null;
    }
 
    //向左补位0
    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(str);// 左补0
                // sb.append(str).append("0");//右补0
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }
 
 
    /**
     * //解析stcd设备编号
     * 功能描述:
     *
     * @param: [stcd]
     * @return: java.lang.String
     * @auther: Administrator
     * @date: 2020/7/23 0023 15:52
     */
    public static String stcdHex2bcd(String stcd) {
        // 2222  --> ae 08 00
        //截取stcd 的最后4个字母 转成bcd
        stcd=Integer.toHexString(Integer.parseInt(stcd));
        stcd=addZeroForNum(stcd,6);
 
        return stcd.substring(4,6)+stcd.substring(2,4)+stcd.substring(0,2);
    }
 
    /**
     * 功能描述: 解析stc  将 bcd 转成hex
     *
     * @param: [stcd]
     * @return: java.lang.String
     * @auther: Administrator
     * @date: 2020/8/4 0004 11:48
     */
    public static String stcdbcd2hex(String stcd) {
 
        //010203 000005
        //截取 hou6位数据 转成 hex
        String bcdnumber = stcd.substring(6, 12);
        Integer hex = Integer.parseInt(stcd.substring(6, 12));
        String hexnumber = addZeroForNum(encodeHEX(hex), 4);
        String data = stcd.substring(0, 6) + hexnumber;
        return data;
    }
 
 
    /**
     * 功能描述:
     *
     * @param: [receiveStr]
     * @return: java.lang.Boolean
     * @auther: Administrator
     * @date: 2020/7/29 0029 10:36
     */
    //CRC 校验 只需要输入完整的字符串 就可以验证是否成功  低位在前 高位在后
    public static Boolean determineCRC16(String receiveStr) {
        String data = receiveStr.substring(receiveStr.length() - 4);
        String RSC16 = getCRC(receiveStr.substring(0, receiveStr.length() - 4)).toLowerCase();
        if (data.equals(RSC16)) {
            return true;
        } else {
            return false;
        }
    }
 
 
    //高位在前低位在后
    public static Boolean determineCRC16High(String receiveStr) {
        //数据全部变成小写
        receiveStr = receiveStr.toLowerCase();
        String data = receiveStr.substring(receiveStr.length() - 4);
        String RSC16 = getCRChigh(receiveStr.substring(0, receiveStr.length() - 4)).toLowerCase();
        if (data.equals(RSC16)) {
            return true;
        } else {
            return false;
        }
    }
 
 
    //将10进制 转16
    public static String encodeHEX(Integer numb) {
 
        String hex = Integer.toHexString(numb);
        return hex;
 
    }
 
 
    /**
     * 功能描述:  16进制转2进制
     *
     * @param: [hexString]
     * @return: java.lang.String
     * @auther: Administrator
     * @date: 2020/8/6 0006 9:34
     */
//    public static String hexString2binaryString(String hexString) {
//
//        if (hexString == null || hexString.length() % 2 != 0)
//            return null;
//        String bString = "", tmp;
//        for (int i = 0; i < hexString.length(); i++) {
//            tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
//            bString += tmp.substring(tmp.length() - 4);
//        }
//        return bString;
//    }
    //------------------------------------------------------
 
    /**
     * 功能描述: 2进制转16进制
     *
     * @param: [bString]
     * @return: java.lang.String
     * @auther: Administrator
     * @date: 2020/8/6 0006 9:34
     */
 
    public static String binaryString2hexString(String bString) {
 
 
        StringBuffer tmp = new StringBuffer();
        int iTmp = 0;
        for (int i = 0; i < bString.length(); i += 4) {
            iTmp = 0;
            for (int j = 0; j < 4; j++) {
                iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
            }
            tmp.append(Integer.toHexString(iTmp));
        }
        return tmp.toString();
    }
 
 
    /**
     * 功能描述:   将16进制转ascII 码
     *
     * @param: [hex]
     * @return: java.lang.String
     * @auther: Administrator
     * @date: 2020/8/24 0024 15:37
     */
    public static String convertHexToString(String hex) {
 
        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();
        //49204c6f7665204a617661 split into two characters 49, 20, 4c...
        for (int i = 0; i < hex.length() - 1; i += 2) {
            //grab the hex in pairs
            String output = hex.substring(i, (i + 2));
            //convert hex to decimal
            int decimal = Integer.parseInt(output, 16);
            //convert the decimal to character
            sb.append((char) decimal);
 
            temp.append(decimal);
        }
 
        return sb.toString();
    }
 
 
    /**
     * 功能描述:  解析字符串  YYMMDDHHmmSS  直接解析时间
     *
     * @param: [dateStr]
     * @return: java.time.LocalDateTime
     * @auther: Administrator
     * @date: 2020/8/25 0025 16:24
     */
    public static LocalDateTime Generationtime(String dateStr) {
        String year = "20" + dateStr.substring(0, 2).replaceAll("^(0+)", "");
        String month = dateStr.substring(2, 4).replaceAll("^(0+)", "");
        String daymonth = dateStr.substring(4, 6).replaceAll("^(0+)", "");
        LocalDate localDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(daymonth));
        String hour = dateStr.substring(6, 8);
        String minute = dateStr.substring(8, 10);
        String second = dateStr.substring(10, 12);
        LocalDateTime datetime = LocalDateTime.of(localDate, LocalTime.parse(hour + ":" + minute + ":" + second));
        return datetime;
    }
 
 
    public static LocalDateTime Generationtime_ss(String dateStr) {
        String year = "20" + dateStr.substring(0, 2).replaceAll("^(0+)", "");
        String month = dateStr.substring(2, 4).replaceAll("^(0+)", "");
        String daymonth = dateStr.substring(4, 6).replaceAll("^(0+)", "");
        LocalDate localDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(daymonth));
        String hour = dateStr.substring(6, 8);
        String minute = dateStr.substring(8, 10);
        // String second = dateStr.substring(10, 12);
        LocalDateTime datetime = LocalDateTime.of(localDate, LocalTime.parse(hour + ":" + minute + ":" + "00"));
        return datetime;
 
 
    }
 
 
    /**
     * @param hexStr
     * @return
     * @description 将16进制转换为二进制
     */
//    public static byte[] parseHexStr2Byte(String hexStr) {
//        if (hexStr.length() < 1)
//            return null;
//        byte[] result = new byte[hexStr.length() / 2];
//        for (int i = 0; i < hexStr.length() / 2; i++) {
//            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
//            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
//            result[i] = (byte) (high * 16 + low);
//        }
//
//        return result;
//    }
 
 
    private static String[] binaryArray =
            {"0000", "0001", "0010", "0011",
                    "0100", "0101", "0110", "0111",
                    "1000", "1001", "1010", "1011",
                    "1100", "1101", "1110", "1111"};
 
    private static String hexStr = "0123456789abcdef";
 
    public static String bytes2BinaryStr(byte[] bArray) {
 
        String outStr = "";
        int pos = 0;
        for (byte b : bArray) {
            //高四位
            pos = (b & 0xF0) >> 4;
            outStr += binaryArray[pos];
            //低四位
            pos = b & 0x0F;
            outStr += binaryArray[pos];
        }
        return outStr;
 
    }
 
 
    public static byte[] HexStringToBinary(String hexString) {
        //hexString的长度对2取整,作为bytes的长度
        int len = hexString.length() / 2;
        byte[] bytes = new byte[len];
        byte high = 0;//字节高四位
        byte low = 0;//字节低四位
 
        for (int i = 0; i < len; i++) {
            //右移四位得到高位
            high = (byte) ((hexStr.indexOf(hexString.charAt(2 * i))) << 4);
            low = (byte) hexStr.indexOf(hexString.charAt(2 * i + 1));
            bytes[i] = (byte) (high | low);//高地位做或运算
        }
        return bytes;
    }
 
 
//    public static String binaryString2hexString1(String bString) {
//        if (bString == null || bString.equals("") || bString.length() % 8 != 0)
//            return null;
//        StringBuffer tmp = new StringBuffer();
//        int iTmp = 0;
//        for (int i = 0; i < bString.length(); i += 4) {
//            iTmp = 0;
//            for (int j = 0; j < 4; j++) {
//                iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
//            }
//            tmp.append(Integer.toHexString(iTmp));
//        }
//        return tmp.toString();
//    }
 
 
    public static String fatsDay() {
        Calendar currCal = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, currCal.get(Calendar.YEAR));
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String firstday = format.format(time);
        return firstday;
    }
 
 
//    public static Pushstatus SendSms(String stcd, String phonenumber) {
//        Pushstatus pushstatus=new Pushstatus();
//
//        try {
//            CloseableHttpClient client = null;
//            CloseableHttpResponse response = null;
//            try {
//                List<BasicNameValuePair> formparams = new ArrayList<>();
//                formparams.add(new BasicNameValuePair("Account","17582900772"));
//                formparams.add(new BasicNameValuePair("Pwd","36514ef9d3fe42e7791eb2ca4"));//登录后台 首页显示
//                formparams.add(new BasicNameValuePair("Content","请回复大写字母‘Y’即可开启水泵,回复大写‘N’即可关闭水泵   设备编号:"+stcd+"请核对后回复,【有效时间10分钟】"));
//                formparams.add(new BasicNameValuePair("TemplateId","191576"));
//                formparams.add(new BasicNameValuePair("Mobile",phonenumber));
//                formparams.add(new BasicNameValuePair("SignId","389157"));//登录后台 添加签名获取id
//                HttpPost httpPost = new HttpPost("http://api.feige.ee/SmsService/Send");
//                httpPost.setEntity(new UrlEncodedFormEntity(formparams,"UTF-8"));
//                client = HttpClients.createDefault();
//                response = client.execute(httpPost);
//                HttpEntity entity = response.getEntity();
//                String result = EntityUtils.toString(entity);
//                System.out.println(result);
//                JSONObject json = JSONObject.parseObject(result);
//                pushstatus.setCode(json.get("Code").toString());
//                pushstatus.setMessage(json.get("Message").toString());
//            } finally {
//                if (response != null) {
//                    response.close();
//                }
//                if (client != null) {
//                    client.close();
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return  pushstatus;
//    }
//
//
//    //直接获取 上行数据
//    public static  Pushstatus  Pusphone(){
//        Pushstatus pushstatus=new Pushstatus();
//        try {
//            CloseableHttpClient client = null;
//            CloseableHttpResponse response = null;
//            try {
//                List<BasicNameValuePair> formparams = new ArrayList<>();
//                formparams.add(new BasicNameValuePair("Account","17582900772"));
//                formparams.add(new BasicNameValuePair("Pwd","36514ef9d3fe42e7791eb2ca4"));//登录后台 首页显示
//                HttpPost httpPost = new HttpPost("http://api.feige.ee/ReportMo/Mo");
//                httpPost.setEntity(new UrlEncodedFormEntity(formparams,"UTF-8"));
//                client = HttpClients.createDefault();
//                response = client.execute(httpPost);
//                HttpEntity entity = response.getEntity();
//                String result = EntityUtils.toString(entity);
//                System.out.println(result);
//              /*  String a1="{\"MoList\":null,\"Code\":10012,\"Message\":\"暂无上行\"}";*/
//                /*String a="{\"MoList\":[{\"Mobile\":\"17582900772\",\"Content\":\"Y\",\"Times\":1615551076,\"ExtNo\":\"97411\"}],\"Code\":0,\"Message\":\"OK\"}";*/
//                JSONObject json = JSONObject.parseObject(result);
//                pushstatus.setCode(json.get("Code").toString());
//                pushstatus.setMessage(json.get("Message").toString());
//
//                if (json.get("Code").toString().equals("0")){
//                 /*List<FeigedataEntity> feigedata=  ( List<FeigedataEntity> )  json.get("MoList");*/
//                    List<FeigedataEntity> feigedata=   ( List<FeigedataEntity> )   JSONArray.parseArray(json.get("MoList").toString(),FeigedataEntity.class);
//                    pushstatus.setFeigedataEntityList(feigedata);
//                    //向数据库中插入数据
//                }
//
//
//
//
//            } finally {
//                if (response != null) {
//                    response.close();
//                }
//                if (client != null) {
//                    client.close();
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return  pushstatus;
//    }
 
 
    public static String dateToStamp(String s) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String res = "";
        if (!"".equals(s)) {
            try {
                res = String.valueOf(sdf.parse(s).getTime() / 1000);
            } catch (Exception e) {
                System.out.println("传入了null值");
            }
        } else {
            long time = System.currentTimeMillis();
            res = String.valueOf(time / 1000);
        }
 
        return res;
    }
 
 
    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(int time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = format.format(new Date(time * 1000L));
        System.out.println("日期格式---->" + times);
        return times;
    }
 
 
    public static long GetDateName(String endtime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss");
 
        String result = "";
        long nm = 60;
        formatter.format(new Date());
// 计算差多少分钟
        Long aa = new Date().getTime();
        String aa1 = aa.toString().substring(0, 10);
 
        long diff = Long.parseLong(aa1) - Long.parseLong(endtime);
        long min = diff / nm;
//        System.out.print(new Date().getTime());
 
        if (min <= 30) { // 半小时内发布
            result = "半小时前";
        } else if (min <= 60) { // 1小时内发布
            result = "1小时前";
        } else if (min <= 120) { // 2小时内发布
            result = "2小时前";
        } else if (min <= 180) { // 3小时内发布
            result = "3小时前";
        } else if (min <= 240) { // 4小时内发布
            result = "4小时前";
        } else if (min <= 300) { // 5小时内发布
            result = "5小时前";
        } else if (min <= 360) { // 6小时内发布
            result = "6小时前";
        } else if (min <= 420) { // 7小时内发布
            result = "7小时前";
        } else if (min <= 480) { // 8小时内发布
            result = "8小时前";
        } else if (min <= 540) { // 9小时内发布
            result = "9小时前";
        } else if (min <= 600) { // 10小时内发布
            result = "10小时前";
        } else if (min <= 660) { // 11小时内发布
            result = "11小时前";
        } else if (min <= 720) { // 12小时内发布
            result = "12小时前";
        }/*else{
            result = formatter.format(discuzcreatetime);
        }*/
        return min;
    }
 
 
    public static String getDate(Date endDate, Date nowDate) {
 
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
// long ns = 1000;
 
        long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
        long day = diff / nd;
// 计算差多少小时
        long hour = diff % nd / nh;
// 计算差多少分钟
        long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
 
 
    //将BCD的ip解析成HEXip
    public static String ipToLong(String ipString) {
        if (StringUtils.isEmpty(ipString)) {
            return null;
        }
        String host = ipString.substring(ipString.indexOf(":") + 1, ipString.length());
        //截取端口
        ipString = ipString.substring(0, ipString.indexOf(":"));
        String[] ip = ipString.split("\\.");
        StringBuffer sb = new StringBuffer();
        for (String str : ip) {
            String hexstr = Integer.toHexString(Integer.parseInt(str));
            sb.append(addZeroForNum(hexstr, 2));
        }
        String hoststr = Integer.toHexString(Integer.parseInt(host));
        return sb.toString() + addZeroForNum(hoststr, 4);
    }
 
 
    public static String iphex2bcd(String ipString) {
        String ip1 = Integer.toString(Integer.parseInt(ipString.substring(0, 2), 16));
        String ip2 = Integer.toString(Integer.parseInt(ipString.substring(2, 4), 16));
        String ip3 = Integer.toString(Integer.parseInt(ipString.substring(4, 6), 16));
        String ip4 = Integer.toString(Integer.parseInt(ipString.substring(6, 8), 16));
        String host = Integer.toString(Integer.parseInt(ipString.substring(8, 12), 16));
        return ip1 + "." + ip2 + "." + ip3 + "." + ip4 + ":" + host;
    }
 
    public static  String replace (String fileStr,List<String> pieceList){
        for (String piece: pieceList){
            int number;
            if ((piece.length()-1)%2==0){
                number=piece.length()-1;
            }else{
                number=piece.length();
            }
             String replace="40"+addZeroForNum(piece.substring(1),6);
            fileStr=fileStr.replaceAll(piece,replace);
        }
        return fileStr;
    }
 
 
 
    
 
 
 
    public static void main(String[] args) {
        boolean data = determineCRC16High("11223344554BD1");
 
    }
 
 
}