liurunyu
2024-10-19 8456b4e16a9fe4284c45b6d56d7dabeea1cb18e9
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
package com.dy.common.util;
 
/**
 * @Author: liurunyu
 * @Date: 2024/7/27 15:50
 * @Description
 */
public class BCDUtil {
 
    /**
     *
     * @param num
     * @return
     */
    public static byte[] int2BCD_LE(int num){
        int len =  (int) Math.log10(num) + 1 ;
        byte[] bs = null ;
        if(len % 2 == 0){
            bs = new byte[len / 2] ;
        }else{
            bs = new byte[len / 2 + 1] ;
        }
        int index = 0 ;
        while(num > 0){
            int yu = num % 100 ;
            bs[index++] = (byte)(((yu/10) << 4) + (yu%10)) ;
            num = num / 100 ;
        }
        return bs ;
    }
 
    /**
     *
     * @param num
     * @return
     */
    public static byte[] int2BCD_BE(int num){
        int len =  (int) Math.log10(num) + 1 ;
        byte[] bs = null ;
        if(len % 2 == 0){
            bs = new byte[len / 2] ;
        }else{
            bs = new byte[len / 2 + 1] ;
        }
        int index = bs.length - 1 ;
        while(num > 0){
            int yu = num % 100 ;
            bs[index--] = (byte)(((yu/10) << 4) + (yu%10)) ;
            num = num / 100 ;
        }
        return bs ;
    }
 
    /**
     *
     * @param num
     * @return
     */
    public static byte[] long2BCD_LE(long num){
        int len =  (int) Math.log10(num) + 1 ;
        byte[] bs = null ;
        if(len % 2 == 0){
            bs = new byte[len / 2] ;
        }else{
            bs = new byte[len / 2 + 1] ;
        }
        int index = 0 ;
        while(num > 0){
            long yu = num % 100 ;
            bs[index++] = (byte)(((yu/10) << 4) + (yu%10)) ;
            num = num / 100 ;
        }
        return bs ;
    }
 
    /**
     *
     * @param num
     * @return
     */
    public static byte[] long2BCD_BE(long num){
        int len =  (int) Math.log10(num) + 1 ;
        byte[] bs = null ;
        if(len % 2 == 0){
            bs = new byte[len / 2] ;
        }else{
            bs = new byte[len / 2 + 1] ;
        }
        int index = bs.length - 1 ;
        while(num > 0){
            long yu = num % 100 ;
            bs[index--] = (byte)(((yu/10) << 4) + (yu%10)) ;
            num = num / 100 ;
        }
        return bs ;
    }
 
    /**
     *
     * @param numStr
     * @return
     */
    public static byte[] string2BCD_LE(String numStr) throws Exception{
        if (numStr == null || !numStr.matches("\\d*")) {
            throw new Exception("数字转成BCD编码时出错,不是合法数字:" + numStr, null);
        }
        int numLen = numStr.length() ;
        int byteLen = 0 ;
        if(numLen % 2 == 0){
            byteLen = numLen / 2 ;
        }else{
            byteLen = numLen / 2 + 1 ;
        }
 
        int[] tmpInts = new int[byteLen * 2];
        int index = numStr.length() - 1;
        for (int i = 0; i <= tmpInts.length - 1 && index >= 0; i++, index--) {
            tmpInts[i] = numStr.charAt(index) - '0';
        }
        byte[] bs = new byte[byteLen] ;
        for (int i = 0, j = 0; i < byteLen; i++, j++) {
            bs[i] = (byte) (tmpInts[2 * j + 1] * 16 + tmpInts[2 * j]);
        }
 
        return bs ;
    }
 
    /**
     *
     * @param numStr
     * @return
     */
    public static byte[] string2BCD_BE(String numStr) throws Exception{
        if (numStr == null || !numStr.matches("\\d*")) {
            throw new Exception("数字转成BCD编码时出错,不是合法数字:" + numStr, null);
        }
        int numLen = numStr.length() ;
        int byteLen = 0 ;
        if(numLen % 2 == 0){
            byteLen = numLen / 2 ;
        }else{
            byteLen = numLen / 2 + 1 ;
        }
 
        int[] tmpInts = new int[byteLen * 2];
        int index = numStr.length() - 1;
        for (int i = tmpInts.length - 1; i >= 0 && index >= 0; i--, index--) {
            tmpInts[i] = numStr.charAt(index) - '0';
        }
        byte[] bs = new byte[byteLen] ;
        for (int i = 0, j = 0; i < byteLen; i++, j++) {
            bs[i] = (byte) (tmpInts[2 * j] * 16 + tmpInts[2 * j + 1]);
        }
 
        return bs ;
    }
 
    /**
     *
     * @param bs
     * @return
     */
    public static int BCD2Int_LE(byte[] bs){
        int num = 0 ;
        int multiple = 1 ;
        for(int i = 0; i < bs.length; i++){
            num += (((bs[i] >> 4) * 10) + (bs[i] & 0xF)) * multiple ;
            multiple = multiple * 100 ;
        }
        return num ;
    }
 
    /**
     *
     * @param bs
     * @return
     */
    public static int BCD2Int_BE(byte[] bs){
        int num = 0 ;
        int multiple = 1 ;
        for(int i = bs.length-1; i >= 0 ; i--){
            num += (((bs[i] >> 4) * 10) + (bs[i] & 0xF)) * multiple ;
            multiple = multiple * 100 ;
        }
        return num ;
    }
 
    /**
     *
     * @param bs
     * @return
     */
    public static long BCD2Long_LE(byte[] bs){
        long num = 0 ;
        long multiple = 1 ;
        for(int i = 0; i < bs.length; i++){
            num += (((bs[i] >> 4) * 10) + (bs[i] & 0xF)) * multiple ;
            multiple = multiple * 100 ;
        }
        return num ;
    }
 
    /**
     *
     * @param bs
     * @return
     */
    public static long BCD2Long_BE(byte[] bs){
        long num = 0 ;
        long multiple = 1 ;
        for(int i = bs.length-1; i >= 0 ; i--){
            num += (((bs[i] >> 4) * 10) + (bs[i] & 0xF)) * multiple ;
            multiple = multiple * 100 ;
        }
        return num ;
    }
 
    /**
     *
     * @param bs
     * @return
     */
    public static String BCD2String_LE(byte[] bs) throws Exception{
        StringBuilder sb = new StringBuilder();
        for (int i = (bs.length - 1); i >= 0; i--) {
            int value = (bs[i] + 256) % 256;
            sb.append((char) (value / 16 + '0')).append((char) (value % 16 + '0'));
        }
 
        String result = sb.toString();
        if (!result.matches("\\d*")) {
            throw new Exception("解码BCD,但数据非BCD码!");
        }
        return result;
    }
 
    /**
     *
     * @param bs
     * @return
     */
    public static String BCD2String_BE(byte[] bs) throws Exception{
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bs.length ; i++) {
            int value = (bs[i] + 256) % 256;
            sb.append((char) (value / 16 + '0')).append((char) (value % 16 + '0'));
            value++;
        }
        String result = sb.toString();
        if (!result.matches("\\d*")) {
            throw new Exception("解码BCD,但数据(" + result + "非BCD码!");
        }
        return result;
    }
 
    public static void main(String[] args) throws Exception {
        int num = 1234567;
        byte[] bs = int2BCD_LE(num) ;
        int num_ =BCD2Int_LE(bs) ;
        System.out.println(num_);
        bs = int2BCD_BE(num) ;
        num_ = BCD2Int_BE(bs);
        System.out.println(num_);
 
        String str = "12345678901234567890" ;
        bs = string2BCD_LE(str) ;
        String str_ = BCD2String_LE(bs) ;
        System.out.println(str_);
        bs = string2BCD_BE(str) ;
        str_ = BCD2String_BE(bs) ;
        System.out.println(str_);
    }
}