elkers
2025-04-07 bd9808a81b1eafdc75a42c1c9904408dc888061d
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
package com.nanjing.water;
 
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class DemoTest {
 
        private static String dataq="FEFEFEFE687236123413EFF72281EDF6E001FFFEFF01060605040503FF070501FFFE040202FFC9363736373338383838373718F7372137373737372E0D1B81823737373737553725373736373737372732453700362F0037370037373737373718F71F332E3737344F3737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737371F332F3737344F3737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737371F33303737344F3737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737371F33313737344F3737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737371F33323737344F3737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737371F33333737344F3737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737371F33343737344F3737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737370AFF050637370306373737060504370506050704FE0503FF374516";
    public static void main(String[] args) {
 
    }
 
 
    public static String decimalToHex(int decimal) {
        return String.format("%02X", decimal);
    }
 
    // 将16进制字符串转换为字节数组
    public static byte[] hexStringToByteArray(String hexString) {
        int len = hexString.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                    + Character.digit(hexString.charAt(i+1), 16));
        }
        return data;
    }
 
    // 计算累加和
    public static int calculateChecksum(byte[] bytes) {
        BigInteger sum = BigInteger.ZERO;
        for (byte b : bytes) {
            sum = sum.add(BigInteger.valueOf(b & 0xFF));
        }
        return sum.intValue();
    }
 
 
 
    /**
     * 获取当前时间的BCD时间
     * @return
     */
    public static String timeBCD(){
        // 获取当前日期和时间
        LocalDateTime now = LocalDateTime.now();
 
        // 提取年月日时分秒
        int year = now.getYear() % 100; // 取年份的后两位
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        int hour = now.getHour();
        int minute = now.getMinute();
        int second = now.getSecond();
        StringBuilder stringBuilder = new StringBuilder(padZero(year));
        stringBuilder.append(padZero(month))
                .append(padZero(day))
                .append(padZero(hour))
                .append(padZero(minute))
                .append(padZero(second));
        return stringBuilder.toString();
    }
 
    /**
     * 不足两位变成两位
     * @param decimal
     * @return
     */
    public static String padZero(int decimal) {
        return String.format("%02d", decimal);
    }
 
 
    // 原文数据按字节取反,然后再加上38H
    public static String reverseAndAdd38H(String decimalString ,int length) {
 
        // 如果字符串长度小于规定的位数,则在前面添加0,使其达到规定的位数
        while (decimalString.length() < length) {
            decimalString = "0" + decimalString;
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < decimalString.length(); i += 2) {
            // 每次取两个字符作为一个单位
            String unit = decimalString.substring(i, i + 2);
            // 将每个单位解析为16进制数值
            int value = Integer.parseInt(unit, 16);
            // 取反
            value = ~value;
            // 将其减去38H
            value += 0x38;
 
            // 获取最后两位
            int lastTwoDigits = value & 0xFF; // 0xFF是16进制表示的255
            // 将最后两位转换为16进制字符串
            String hexResult = Integer.toHexString(lastTwoDigits);
            // 不足两位的情况补0
            if (hexResult.length() < 2) {
                hexResult = "0" + hexResult;
            }
            // 将结果追加到StringBuilder中
            result.append(hexResult);
        }
        return String.valueOf(result);
    }
 
    private static String hexStringToDecimalInt(StringBuilder hexString) {
        StringBuilder result = new StringBuilder();
 
        // 如果输入的字符串长度为奇数,补0使其长度为偶数
        if (hexString.length() % 2 != 0) {
            hexString = new StringBuilder("0" + hexString);
        }
 
        for (int i = 0; i < hexString.length(); i += 2) {
            // 每次取两个字符作为一个单位
            String unit = hexString.substring(i, i + 2);
            // 将每个单位解析为16进制数值
            int value = Integer.parseInt(unit, 16);
            // 将其减去38H
            value -= 0x38;
            // 取反
            value = ~value;
            // 获取最后两位
            int lastTwoDigits = value & 0xFF; // 0xFF是16进制表示的255
            // 将最后两位转换为16进制字符串
            String hexResult = Integer.toHexString(lastTwoDigits);
            // 不足两位的情况补0
            if (hexResult.length() < 2) {
                hexResult = "0" + hexResult;
            }
            // 将结果追加到StringBuilder中
            result.append(hexResult);
        }
        return String.valueOf(result);
    }
 
    public static String getCurrentTimeInBCD() {
        LocalDateTime now = LocalDateTime.now(); // 获取当前系统时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); // 定义时间格式
        String formattedDateTime = now.format(formatter); // 格式化时间为字符串
        return Long.toHexString(Long.parseLong(formattedDateTime));
    }
 
    public static String hexStringToDecimalString(String hexString) {
        // 将16进制字符串转换为对应的10进制字符串
        String decimalString = new BigInteger(hexString, 16).toString();
        return decimalString;
    }
 
    public static String reverseInPairs(String str) {
        StringBuilder reversed = new StringBuilder();
        // 从字符串末尾开始,每次取两个字符,并添加到reversed StringBuilder 中
        for (int i = str.length() - 2; i >= 0; i -= 2) {
            reversed.append(str.charAt(i)).append(str.charAt(i+1));
        }
        return reversed.toString();
    }
    private static String H16ToASCII(String s){
        // 将字符串按两个字符一组进行分割
        String[] hexPairs = s.split("(?<=\\G..)");
 
        // 将每个十六进制数转换为对应的ASCII字符,并拼接成最终的ASCII字符串
        StringBuilder asciiString = new StringBuilder();
        for (String hexPair : hexPairs) {
            int decimalValue = Integer.parseInt(hexPair, 16);
            asciiString.append((char) decimalValue);
        }
 
        return asciiString.toString();
    }
}