zj
2025-02-25 dd315d5732e14fcf3df71e0cf213cc442bd8607b
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
package kernel.util;
 
/**
 
 *  一些字节处理的工具函数,用于一些比较底层的基于字节的协议转换
 
 */
public abstract class ByteUtils {
    /**
     * 初始化内存字节
     * 
     * @param src
     *            byte[]
     * @param c
     *            byte
     * @param pos
     *            int
     * @param count
     *            int
     * @return byte[]
     */
    public static byte[] memset(byte[] src, byte c, int pos, int count) {
        if (src == null)
            return src;
 
        if (pos < 0 || pos > src.length || count <= 0) {
            return src;
        }
 
        for (int i = 0; i < count && pos + i < src.length; i++) {
            src[pos + i] = c;
        }
 
        return src;
    }
 
    /**
     * 内存字节拷贝
     * 
     * @param dest
     *            byte[]
     * @param pos
     *            int
     * @param src
     *            byte[]
     * @param size
     *            int
     * @return byte[]
     */
    public static byte[] memcpy(byte[] dest, int pos, byte[] src, int size) {
        if (size >= 0) {
            size = size > dest.length ? dest.length : size;
            System.arraycopy(src, 0, dest, pos, size);
        }
        return dest;
    }
 
    /**
     * 字节头部填充
     * 
     * @param src
     *            byte[]
     * @param length
     *            int
     * @param fill
     *            byte
     * @return byte[]
     */
    public static byte[] fillHead(byte[] src, int length, byte fill) {
        if (src.length >= length) {
            return src;
        }
 
        byte[] dest = new byte[length];
        ByteUtils.memset(dest, fill, 0, length);
        ByteUtils.memcpy(dest, length > src.length ? length - src.length : 0,
                src, src.length);
        return dest;
    }
 
    /**
     * 字节尾部填充
     * 
     * @param src
     *            byte[]
     * @param length
     *            int
     * @param fill
     *            byte
     * @return byte[]
     */
    public static byte[] fillTail(byte[] src, int length, byte fill) {
        if (src.length >= length) {
            return src;
        }
 
        byte[] dest = new byte[length];
        ByteUtils.memset(dest, fill, 0, length);
        ByteUtils.memcpy(dest, 0, src, src.length);
        return dest;
    }
 
    /**
     * 插入字节到目标字节数组,使用者须保证目标空间足够,后面的字节被覆盖
     * 
     * @param dest
     *            byte[]
     * @param src
     *            byte[]
     * @param pos
     *            int
     * @param length
     *            int
     * @return byte[]
     */
    public static byte[] insert(byte[] dest, byte[] src, int pos, int length) {
        if (length > dest.length) {
            length = dest.length;
        }
 
        for (int i = 0; i < pos + length; i++) {
            if (i >= pos && i < pos + length) {
                if (i + length < dest.length) {
                    dest[i + length] = dest[i];
                }
                if (i - pos < src.length) {
                    dest[i] = src[i - pos];
                }
            }
        }
 
        return dest;
    }
 
    /**
     * 复制字节到目标字节数组,使用者须保证目标空间足够
     * 
     * @param dest
     *            byte[]
     * @param src
     *            byte[]
     * @param pos
     *            int
     * @param length
     *            int
     * @return byte[]
     */
    public static byte[] copyto(byte[] dest, byte[] src, int pos, int length) {
        if (length > src.length) {
            length = src.length;
        }
 
        if (dest == null || dest.length < pos + length) {
            byte[] buf = new byte[pos + length];
            memcpy(buf, 0, dest, pos);
            dest = buf;
        }
 
        for (int i = 0; i < length; i++) {
            dest[pos + i] = src[i];
        }
 
        return dest;
    }
 
    /**
     * 从源字节数组指定位置复制字节到目标字节数组,使用者须保证目标空间足够
     * 
     * @param dest
     *            byte[]
     * @param src
     *            byte[]
     * @param pos
     *            int
     * @param length
     *            int
     * @return byte[]
     */
    public static byte[] copyfrom(byte[] dest, byte[] src, int pos, int length) {
        if (length > src.length) {
            length = src.length;
        }
 
        if (dest == null || dest.length < length) { // 只能通过返回参数返回,无法通过入参返回,与C语言不同。
            dest = new byte[length];
        }
 
        for (int i = 0; i < length; i++) {
            dest[i] = src[pos + i];
        }
 
        return copy(dest, 0, length); // 返回一个新建数组
    }
 
    /**
     * 删除字节内容,返回删除后的内容
     * 
     * @param src
     *            byte[]
     * @param pos
     *            int
     * @param length
     *            int
     * @return byte[] 删除一些字节的新数组
     */
    public static byte[] delete(byte src[], int pos, int length) {
        if (src == null) {
            return null;
        }
 
        if (pos > src.length || pos < 0 || length < 0) { // 非法参数
            return src;
        }
 
        if (pos + length > src.length) {
            length = src.length - pos;
        }
 
        byte[] dest = new byte[src.length - length];
        for (int i = 0; i < src.length; i++) {
            if (i - pos >= 0 && i - pos < length) {
                continue;
            }
            dest[i] = src[i];
        }
        return dest;
    }
 
    /**
     * 字节拷贝,返回拷贝内容
     * 
     * @param src
     *            byte[]
     * @param pos
     *            int
     * @param size
     *            int
     * @return byte[]
     */
    public static byte[] copy(byte[] src, int pos, int size) {
        if (pos < 0 || pos > src.length || size <= 0) { // 非法参数
            return null;
        }
 
        byte[] dest = new byte[src.length - pos < size ? src.length - pos
                : size];
        for (int i = 0; i < size && pos + i < src.length; i++) {
            dest[i] = src[pos + i];
        }
 
        return dest;
    }
 
    /**
     * 转换成16进制的格式
     * 
     * @param length
     *            int
     * @param hexlen
     *            int
     * @param fillhead
     *            byte
     * @return byte[]
     */
    public static byte[] toHexBytes(int length, int hexlen, byte fillhead) {
        if (hexlen <= 0) {
            return null;
        }
 
        byte dest[] = new byte[hexlen];
        ByteUtils.memset(dest, fillhead, 0, hexlen);
        byte[] src = Integer.toHexString(length).getBytes();
        System.arraycopy(src, 0, dest, hexlen - src.length > 0 ? hexlen
                - src.length : 0, src.length);
 
        return dest;
    }
 
    /**
     * 
     * @param b
     *            byte[]
     * @return int
     */
    public static int bytes2int(byte[] b) {
        // byte[] b=new byte[]{1,2,3,4};
        int mask = 0xff;
        int temp = 0;
        int res = 0;
        for (int i = 0; i < 4; i++) {
            res <<= 8;
            temp = b[i] & mask;
            res |= temp;
        }
        return res;
    }
 
    /**
     * 
     * @param b
     *            byte
     * @return String
     */
    public static String byte2hex(byte b) {
        String hex = Integer.toHexString((int) b & 0xff);
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        return hex;
    }
 
    /**
     * 
     * @param hex
     *            String
     * @return byte
     */
    public static byte hex2byte(String hex) {
        return (byte) (Integer.parseInt(hex, 16) & 0xff);
    }
 
    /**
     * 
     * @param hex
     *            String
     * @return byte[]
     */
    public static byte[] hex2bytes(String hex) {
        if (hex == null || hex.trim().length() == 0) {
            return null;
        }
 
        byte[] dest = new byte[hex.length() / 2];
        for (int i = 0; i < hex.length() / 2; i++) {
            String h = hex.substring(2 * i, 2 * i + 2);
            dest[i] = hex2byte(h);
        }
 
        return dest;
    }
 
    /**
     * 
     * @param bytes
     *            byte[]
     * @return String
     */
    public static String bytes2hex(byte[] bytes) {
        StringBuffer sb = null;
        if (bytes == null || bytes.length == 0) {
            return null;
        }
 
        sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(byte2hex(bytes[i]));
        }
 
        return sb.toString();
    }
 
    /**
     * 
     * @param num
     *            int
     * @return byte[]
     */
    public static byte[] int2bytes(int num) {
        byte[] b = new byte[4];
        // int mask=0xff;
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) (num >>> (24 - i * 8));
            // System.out.print(Integer.toHexString((int)b[i]));
        }
        return b;
    }
 
    /**
     * 
     * @param num
     *            int
     * @return byte[]
     */
    public static byte[] short2bytes(int num) {
        num &= 0xffff;
        byte[] b = new byte[2];
        // int mask=0xff;
        for (int i = 0; i < 2; i++) {
            b[i] = (byte) (num >>> (8 - i * 8));
            // System.out.print(Integer.toHexString((int)b[i]));
        }
        return b;
    }
 
    /**
     * byte2short
     * 
     * @param len
     *            byte[]
     * @return int
     */
    // public static int byte2short(byte[] b) {
    // int mask = 0xff;
    // int temp = 0;
    // int res = 0;
    // for (int i = 0; i < 2; i++) {
    // res <<= 8;
    // temp = b[i] & mask;
    // res |= temp;
    // }
    // return res;
    // }
    public static int byte2short(byte[] b) {
        int mask = 0xff;
        int temp = 0;
        int res = 0;
        for (int i = 0; i < 2; i++) {
            res = ((res << 8) & 0xffff);
            temp = b[i] & mask;
            res |= temp;
        }
        return res & 0xffff;
    }
 
    /**
     * 
     * @param h
     *            int
     * @return int
     */
    public static int htonl(int h) {
        int nl = 0;
        int b = 0;
 
        for (int i = 0; i < 4; i++) {
            b = (h << (24 - i * 8)) & 0xff000000;
            // System.out.println(Integer.toHexString(b));
            // System.out.println(Integer.toHexString(b >> (i * 8)));
            nl = nl + (b >> (i * 8));
        }
 
        // /System.out.println(Integer.toHexString(nl));
        return nl;
    }
 
    public static byte[] htonlbytes(int h) {
        // return int2bytes(htonl(h));
        return int2bytes(h);
    }
 
    /**
     * 
     * @param h
     *            int
     * @return int
     */
    public static int htons(int h) {
        int ns = 0;
        int b = 0;
        for (int i = 0; i < 2; i++) {
            b = (h << (8 - i * 8)) & 0xff00;
            // System.out.println(Integer.toHexString(b));
            // System.out.println(Integer.toHexString(b >> 8));
            ns = ns + (b >> (i * 8));
        }
 
        // System.out.println(Integer.toHexString(ns));
        return ns;
    }
 
    public static byte[] htonsbytes(int h) {
        // return short2bytes(htons(h));
        return short2bytes(h);
    }
 
    /**
     * 
     * @param n
     *            int
     * @return int
     */
    public static int ntohl(int n) {
        return htonl(n);
    }
 
    public static int nbytestohl(byte[] n) {
        // return ntohl(bytes2int(n));
        return bytes2int(n);
    }
 
    public static int ntohs(int n) {
        return htons(n);
    }
 
    public static int nbytestohs(byte[] n) {
        // return ntohs(byte2short(n));
        return byte2short(n);
    }
 
    // /**
    // * getZteIHLRChecksum
    // * @param msg char[]
    // * @return char[]
    // */
    // public static char[] getZteIHLRChecksum(char[] msg) {
    // int i = 0, j = 0;
    // char[] checksum = new char[4];
    //
    // for (i = 0; i < msg.length / 4; i++) {
    // for (j = 0; j < 4; j++) {
    // checksum[j] ^= msg[i * 4 + j];
    // }
    // }
    //
    // for (j = 0; j < msg.length % 4; j++) {
    // checksum[j] ^= msg[i * 4 + j];
    // }
    //
    // for (i = 0; i < 4; i++) {
    // int k = ~checksum[i] & 0xff;
    // checksum[i] = (char) k;
    // }
    //
    // StringBuffer sb = new StringBuffer();
    // for (i = 0; i < 4; i++) {
    // String s = Integer.toHexString(checksum[i] & 0xff).toUpperCase();
    // if (s.length() < 2) {
    // sb.append("0").append(s);
    // }
    // else {
    // sb.append(s);
    // }
    // }
    //
    // return sb.toString().toCharArray();
    // }
 
    // /**
    // * getZtePCSChecksum
    // * @param msg char[]
    // * @return char[]
    // */
    // public static char[] getZtePCSChecksum(char[] msg) {
    // int i = 0, j = 0;
    // char[] checksum = new char[4];
    //
    // for (i = 2; i < (msg.length - 8) / 4; i++) {
    // for (j = 0; j < 4; j++) {
    // checksum[j] ^= msg[i * 4 + j];
    // }
    // }
    //
    // for (i = 0; i < 4; i++) {
    // int k = ~checksum[i] & 0xff;
    // checksum[i] = (char) k;
    // }
    //
    // StringBuffer sb = new StringBuffer();
    // for (i = 0; i < 4; i++) {
    // String s = Integer.toHexString(checksum[i] & 0xff).toUpperCase();
    // if (s.length() < 2) {
    // sb.append("0").append(s);
    // }
    // else {
    // sb.append(s);
    // }
    // }
    //
    // return sb.toString().toCharArray();
    // }
 
    /**
     * 为比对字节好用,显示输出格尺
     */
    public static void showGrid() {
        for (int i = 0; i < 10; i++) {
            System.out.print("0123456789");
        }
        System.out.println();
    }
 
    /**
     * 
     * @param buf
     *            byte[]
     */
    public static void showBytes(byte[] buf) {
        if (buf == null) {
            buf = new byte[0];
        }
        String hex = "";
        byte[] s = new byte[16];
        System.out.println("[BYTES INFO] : LENGTH = " + buf.length + "(0x"
                + Integer.toHexString(buf.length) + ")");
        for (int i = 0; i < buf.length; i++) {
            if (i % 16 == 0) { // 行号
                hex = Integer.toHexString((i & 0xff) / 16); // hex 字符
                if (hex.length() == 1) {
                    hex = "0" + hex;
                }
                System.out.print("[" + hex + "]  ");
            }
 
            hex = Integer.toHexString(((byte) buf[i]) & 0xff); // hex 字符
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            System.out.print(hex + " "); // 输出HEX
            s[i % 16] = buf[i];
 
            if (i != 0 && i % 8 == 7 && i % 16 != 15) { // 8 字节
                System.out.print("  ");
            }
 
            if (i != 0 && (i % 16 == 15 || i == buf.length - 1)) { // 16字节或结束
                if (i % 16 != 15) { // 补空格
                    for (int k = i % 16; k < 16; k++) {
                        if (k == 7) {
                            System.out.print("  ");
                        } else {
                            System.out.print("   ");
                        }
                    }
                }
 
                System.out.print("  "); // 输出可打印字符
                byte c = 126;
                for (int j = 0; j < i % 16 + 1; j++) {
                    if (s[j] == 0 || s[j] <= ' ' || s[j] > c) {
                        s[j] = '.';
                    }
                    System.out.print((char) s[j]);
                }
 
                System.out.print("\n"); // 输出换行
            }
        }
 
        System.out.println("");
    }
 
    /**
     * 
     * @param args
     *            String[]
     */
    // @SuppressWarnings("unused")
    // public static void main(String[] args) {
    // // /byte[] hex = hex2bytes("013233");
    // // /String hhh = bytes2hex(hex);
    //
    // String dd =
    // "SCSC003C1.00JS123456UASUAS  00000000DLGCON    00000001TXBEG     HBHB";
    // // C5F6F7B2
    // // /char[] s = dd.toCharArray();
    // showBytes(dd.getBytes());
    // //
    // System.out.println(ByteUtils.getZteIHLRChecksum("HBHB".toCharArray()));
    // // // it
    // // is ok. B7BDB7BD
    // // System.out.println(ByteUtils.getZteIHLRChecksum(s)); // it is ok.
    // // System.out.println(ByteUtils.getZtePCSChecksum(s));
    //
    // byte[] length = new byte[20];
    // byte[] to = new byte[10];
    // ByteUtils.memset(length, (byte) '0', 0, 20);
    // ByteUtils.memcpy(to, 0, length, 20);
    // System.out.println(new String(ByteUtils.toHexBytes(255, 2, (byte) '0')));
    // System.out.println(new String(ByteUtils.fillHead(to, 20, (byte) '1')));
    // System.out.println(new String(ByteUtils.fillTail(to, 15, (byte) '9')));
    // System.out.println(new String(ByteUtils.copyto(to, "hello!".getBytes(),
    // 0, 10)));
    // System.out.println(new String(ByteUtils.copyto(to,
    // "Hello world!".getBytes(), 2, 15)));
    //
    // System.out.println(new String(ByteUtils.copy(to, 5, 6)));
    // System.out.println(new String(ByteUtils.delete("Hello world".getBytes(),
    // 5, 6)));
    // System.out.println(new String(ByteUtils.insert(to, "Gogogo!".getBytes(),
    // 5, 5)));
    // ByteUtils.memset(length, (byte) '5', 10, 10);
    // System.out.println(new String(length));
    // System.out.println(new String(ByteUtils.insert(length,
    // "Gogogo!".getBytes(), 8, 8)));
    //
    // ByteUtils.showGrid();
    //
    // System.out.println(new String(int2bytes(0x1c1d1e1f)));
    //
    // int n = htonl(0x12345678);
    // System.out.println(Integer.toHexString(n));
    // n = ntohl(n);
    // System.out.println(Integer.toHexString(n));
    //
    // n = htons(0x1234);
    // System.out.println(Integer.toHexString(n));
    // n = htons(n);
    // System.out.println(Integer.toHexString(n));
    //
    // byte[] z = htonlbytes(0x12345678);
    // System.out.println(new String(z));
    // z = htonsbytes(0x1234);
    // System.out.println(new String(z));
    //
    // byte[] j = ByteUtils.short2bytes(0x94d8);
    // System.out.println(Integer.toHexString(0x94d8));
    //
    // int ddd = ByteUtils.byte2short(j);
    // }
}