1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3;
18
19 import java.util.UUID;
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 public class Conversion {
64
65 private static final boolean[] TTTT = {true, true, true, true};
66 private static final boolean[] FTTT = {false, true, true, true};
67 private static final boolean[] TFTT = {true, false, true, true};
68 private static final boolean[] FFTT = {false, false, true, true};
69 private static final boolean[] TTFT = {true, true, false, true};
70 private static final boolean[] FTFT = {false, true, false, true};
71 private static final boolean[] TFFT = {true, false, false, true};
72 private static final boolean[] FFFT = {false, false, false, true};
73 private static final boolean[] TTTF = {true, true, true, false};
74 private static final boolean[] FTTF = {false, true, true, false};
75 private static final boolean[] TFTF = {true, false, true, false};
76 private static final boolean[] FFTF = {false, false, true, false};
77 private static final boolean[] TTFF = {true, true, false, false};
78 private static final boolean[] FTFF = {false, true, false, false};
79 private static final boolean[] TFFF = {true, false, false, false};
80 private static final boolean[] FFFF = {false, false, false, false};
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public static char binaryBeMsb0ToHexDigit(final boolean[] src) {
97 return binaryBeMsb0ToHexDigit(src, 0);
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public static char binaryBeMsb0ToHexDigit(final boolean[] src, final int srcPos) {
117
118 if (Integer.compareUnsigned(srcPos, src.length) >= 0) {
119
120 if (src.length == 0) {
121 throw new IllegalArgumentException("Cannot convert an empty array.");
122 }
123 throw new IndexOutOfBoundsException(srcPos + " is not within array length " + src.length);
124 }
125
126 final int pos = src.length - 1 - srcPos;
127 if (3 <= pos && src[pos - 3]) {
128 if (src[pos - 2]) {
129 if (src[pos - 1]) {
130 return src[pos] ? 'f' : 'e';
131 }
132 return src[pos] ? 'd' : 'c';
133 }
134 if (src[pos - 1]) {
135 return src[pos] ? 'b' : 'a';
136 }
137 return src[pos] ? '9' : '8';
138 }
139 if (2 <= pos && src[pos - 2]) {
140 if (src[pos - 1]) {
141 return src[pos] ? '7' : '6';
142 }
143 return src[pos] ? '5' : '4';
144 }
145 if (1 <= pos && src[pos - 1]) {
146 return src[pos] ? '3' : '2';
147 }
148 return src[pos] ? '1' : '0';
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 public static byte binaryToByte(final boolean[] src, final int srcPos, final byte dstInit, final int dstPos,
167 final int nBools) {
168 if (src.length == 0 && srcPos == 0 || 0 == nBools) {
169 return dstInit;
170 }
171 if (nBools - 1 + dstPos >= 8) {
172 throw new IllegalArgumentException("nBools-1+dstPos is greater or equal to than 8");
173 }
174 byte out = dstInit;
175 for (int i = 0; i < nBools; i++) {
176 final int shift = i + dstPos;
177 final int bits = (src[i + srcPos] ? 1 : 0) << shift;
178 final int mask = 0x1 << shift;
179 out = (byte) ((out & ~mask) | bits);
180 }
181 return out;
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 public static char binaryToHexDigit(final boolean[] src) {
198 return binaryToHexDigit(src, 0);
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 public static char binaryToHexDigit(final boolean[] src, final int srcPos) {
216 if (src.length == 0) {
217 throw new IllegalArgumentException("Cannot convert an empty array.");
218 }
219 if (src.length > srcPos + 3 && src[srcPos + 3]) {
220 if (src[srcPos + 2]) {
221 if (src[srcPos + 1]) {
222 return src[srcPos] ? 'f' : 'e';
223 }
224 return src[srcPos] ? 'd' : 'c';
225 }
226 if (src[srcPos + 1]) {
227 return src[srcPos] ? 'b' : 'a';
228 }
229 return src[srcPos] ? '9' : '8';
230 }
231 if (src.length > srcPos + 2 && src[srcPos + 2]) {
232 if (src[srcPos + 1]) {
233 return src[srcPos] ? '7' : '6';
234 }
235 return src[srcPos] ? '5' : '4';
236 }
237 if (src.length > srcPos + 1 && src[srcPos + 1]) {
238 return src[srcPos] ? '3' : '2';
239 }
240 return src[srcPos] ? '1' : '0';
241 }
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 public static char binaryToHexDigitMsb0_4bits(final boolean[] src) {
258 return binaryToHexDigitMsb0_4bits(src, 0);
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277 public static char binaryToHexDigitMsb0_4bits(final boolean[] src, final int srcPos) {
278 if (src.length > 8) {
279 throw new IllegalArgumentException("src.length>8: src.length=" + src.length);
280 }
281 if (src.length - srcPos < 4) {
282 throw new IllegalArgumentException("src.length-srcPos<4: src.length=" + src.length + ", srcPos=" + srcPos);
283 }
284 if (src[srcPos + 3]) {
285 if (src[srcPos + 2]) {
286 if (src[srcPos + 1]) {
287 return src[srcPos] ? 'f' : '7';
288 }
289 return src[srcPos] ? 'b' : '3';
290 }
291 if (src[srcPos + 1]) {
292 return src[srcPos] ? 'd' : '5';
293 }
294 return src[srcPos] ? '9' : '1';
295 }
296 if (src[srcPos + 2]) {
297 if (src[srcPos + 1]) {
298 return src[srcPos] ? 'e' : '6';
299 }
300 return src[srcPos] ? 'a' : '2';
301 }
302 if (src[srcPos + 1]) {
303 return src[srcPos] ? 'c' : '4';
304 }
305 return src[srcPos] ? '8' : '0';
306 }
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323 public static int binaryToInt(final boolean[] src, final int srcPos, final int dstInit, final int dstPos,
324 final int nBools) {
325 if (src.length == 0 && srcPos == 0 || 0 == nBools) {
326 return dstInit;
327 }
328 if (nBools - 1 + dstPos >= 32) {
329 throw new IllegalArgumentException("nBools-1+dstPos is greater or equal to than 32");
330 }
331 int out = dstInit;
332 for (int i = 0; i < nBools; i++) {
333 final int shift = i + dstPos;
334 final int bits = (src[i + srcPos] ? 1 : 0) << shift;
335 final int mask = 0x1 << shift;
336 out = (out & ~mask) | bits;
337 }
338 return out;
339 }
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356 public static long binaryToLong(final boolean[] src, final int srcPos, final long dstInit, final int dstPos,
357 final int nBools) {
358 if (src.length == 0 && srcPos == 0 || 0 == nBools) {
359 return dstInit;
360 }
361 if (nBools - 1 + dstPos >= 64) {
362 throw new IllegalArgumentException("nBools-1+dstPos is greater or equal to than 64");
363 }
364 long out = dstInit;
365 for (int i = 0; i < nBools; i++) {
366 final int shift = i + dstPos;
367 final long bits = (src[i + srcPos] ? 1L : 0) << shift;
368 final long mask = 0x1L << shift;
369 out = (out & ~mask) | bits;
370 }
371 return out;
372 }
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389 public static short binaryToShort(final boolean[] src, final int srcPos, final short dstInit, final int dstPos,
390 final int nBools) {
391 if (src.length == 0 && srcPos == 0 || 0 == nBools) {
392 return dstInit;
393 }
394 if (nBools - 1 + dstPos >= 16) {
395 throw new IllegalArgumentException("nBools-1+dstPos is greater or equal to than 16");
396 }
397 short out = dstInit;
398 for (int i = 0; i < nBools; i++) {
399 final int shift = i + dstPos;
400 final int bits = (src[i + srcPos] ? 1 : 0) << shift;
401 final int mask = 0x1 << shift;
402 out = (short) ((out & ~mask) | bits);
403 }
404 return out;
405 }
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422 public static int byteArrayToInt(final byte[] src, final int srcPos, final int dstInit, final int dstPos,
423 final int nBytes) {
424 if (src.length == 0 && srcPos == 0 || 0 == nBytes) {
425 return dstInit;
426 }
427 if ((nBytes - 1) * 8 + dstPos >= 32) {
428 throw new IllegalArgumentException("(nBytes-1)*8+dstPos is greater or equal to than 32");
429 }
430 int out = dstInit;
431 for (int i = 0; i < nBytes; i++) {
432 final int shift = i * 8 + dstPos;
433 final int bits = (0xff & src[i + srcPos]) << shift;
434 final int mask = 0xff << shift;
435 out = (out & ~mask) | bits;
436 }
437 return out;
438 }
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455 public static long byteArrayToLong(final byte[] src, final int srcPos, final long dstInit, final int dstPos,
456 final int nBytes) {
457 if (src.length == 0 && srcPos == 0 || 0 == nBytes) {
458 return dstInit;
459 }
460 if ((nBytes - 1) * 8 + dstPos >= 64) {
461 throw new IllegalArgumentException("(nBytes-1)*8+dstPos is greater or equal to than 64");
462 }
463 long out = dstInit;
464 for (int i = 0; i < nBytes; i++) {
465 final int shift = i * 8 + dstPos;
466 final long bits = (0xffL & src[i + srcPos]) << shift;
467 final long mask = 0xffL << shift;
468 out = (out & ~mask) | bits;
469 }
470 return out;
471 }
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488 public static short byteArrayToShort(final byte[] src, final int srcPos, final short dstInit, final int dstPos,
489 final int nBytes) {
490 if (src.length == 0 && srcPos == 0 || 0 == nBytes) {
491 return dstInit;
492 }
493 if ((nBytes - 1) * 8 + dstPos >= 16) {
494 throw new IllegalArgumentException("(nBytes-1)*8+dstPos is greater or equal to than 16");
495 }
496 short out = dstInit;
497 for (int i = 0; i < nBytes; i++) {
498 final int shift = i * 8 + dstPos;
499 final int bits = (0xff & src[i + srcPos]) << shift;
500 final int mask = 0xff << shift;
501 out = (short) ((out & ~mask) | bits);
502 }
503 return out;
504 }
505
506
507
508
509
510
511
512
513
514
515
516
517 public static UUID byteArrayToUuid(final byte[] src, final int srcPos) {
518 if (src.length - srcPos < 16) {
519 throw new IllegalArgumentException("Need at least 16 bytes for UUID");
520 }
521 return new UUID(byteArrayToLong(src, srcPos, 0, 0, 8), byteArrayToLong(src, srcPos + 8, 0, 0, 8));
522 }
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539 public static boolean[] byteToBinary(final byte src, final int srcPos, final boolean[] dst, final int dstPos,
540 final int nBools) {
541 if (0 == nBools) {
542 return dst;
543 }
544 if (nBools - 1 + srcPos >= 8) {
545 throw new IllegalArgumentException("nBools-1+srcPos is greater or equal to than 8");
546 }
547 for (int i = 0; i < nBools; i++) {
548 final int shift = i + srcPos;
549 dst[dstPos + i] = (0x1 & (src >> shift)) != 0;
550 }
551 return dst;
552 }
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568 public static String byteToHex(final byte src, final int srcPos, final String dstInit, final int dstPos,
569 final int nHexs) {
570 if (0 == nHexs) {
571 return dstInit;
572 }
573 if ((nHexs - 1) * 4 + srcPos >= 8) {
574 throw new IllegalArgumentException("(nHexs-1)*4+srcPos is greater or equal to than 8");
575 }
576 final StringBuilder sb = new StringBuilder(dstInit);
577 int append = sb.length();
578 for (int i = 0; i < nHexs; i++) {
579 final int shift = i * 4 + srcPos;
580 final int bits = 0xF & (src >> shift);
581 if (dstPos + i == append) {
582 ++append;
583 sb.append(intToHexDigit(bits));
584 } else {
585 sb.setCharAt(dstPos + i, intToHexDigit(bits));
586 }
587 }
588 return sb.toString();
589 }
590
591
592
593
594
595
596
597
598
599
600
601
602
603 public static boolean[] hexDigitMsb0ToBinary(final char hexDigit) {
604 switch (hexDigit) {
605 case '0':
606 return FFFF.clone();
607 case '1':
608 return FFFT.clone();
609 case '2':
610 return FFTF.clone();
611 case '3':
612 return FFTT.clone();
613 case '4':
614 return FTFF.clone();
615 case '5':
616 return FTFT.clone();
617 case '6':
618 return FTTF.clone();
619 case '7':
620 return FTTT.clone();
621 case '8':
622 return TFFF.clone();
623 case '9':
624 return TFFT.clone();
625 case 'a':
626 case 'A':
627 return TFTF.clone();
628 case 'b':
629 case 'B':
630 return TFTT.clone();
631 case 'c':
632 case 'C':
633 return TTFF.clone();
634 case 'd':
635 case 'D':
636 return TTFT.clone();
637 case 'e':
638 case 'E':
639 return TTTF.clone();
640 case 'f':
641 case 'F':
642 return TTTT.clone();
643 default:
644 throw new IllegalArgumentException("Cannot interpret '" + hexDigit + "' as a hexadecimal digit");
645 }
646 }
647
648
649
650
651
652
653
654
655
656
657
658
659 public static int hexDigitMsb0ToInt(final char hexDigit) {
660 switch (hexDigit) {
661 case '0':
662 return 0x0;
663 case '1':
664 return 0x8;
665 case '2':
666 return 0x4;
667 case '3':
668 return 0xC;
669 case '4':
670 return 0x2;
671 case '5':
672 return 0xA;
673 case '6':
674 return 0x6;
675 case '7':
676 return 0xE;
677 case '8':
678 return 0x1;
679 case '9':
680 return 0x9;
681 case 'a':
682 case 'A':
683 return 0x5;
684 case 'b':
685 case 'B':
686 return 0xD;
687 case 'c':
688 case 'C':
689 return 0x3;
690 case 'd':
691 case 'D':
692 return 0xB;
693 case 'e':
694 case 'E':
695 return 0x7;
696 case 'f':
697 case 'F':
698 return 0xF;
699 default:
700 throw new IllegalArgumentException("Cannot interpret '" + hexDigit + "' as a hexadecimal digit");
701 }
702 }
703
704
705
706
707
708
709
710
711
712
713
714
715
716 public static boolean[] hexDigitToBinary(final char hexDigit) {
717 switch (hexDigit) {
718 case '0':
719 return FFFF.clone();
720 case '1':
721 return TFFF.clone();
722 case '2':
723 return FTFF.clone();
724 case '3':
725 return TTFF.clone();
726 case '4':
727 return FFTF.clone();
728 case '5':
729 return TFTF.clone();
730 case '6':
731 return FTTF.clone();
732 case '7':
733 return TTTF.clone();
734 case '8':
735 return FFFT.clone();
736 case '9':
737 return TFFT.clone();
738 case 'a':
739 case 'A':
740 return FTFT.clone();
741 case 'b':
742 case 'B':
743 return TTFT.clone();
744 case 'c':
745 case 'C':
746 return FFTT.clone();
747 case 'd':
748 case 'D':
749 return TFTT.clone();
750 case 'e':
751 case 'E':
752 return FTTT.clone();
753 case 'f':
754 case 'F':
755 return TTTT.clone();
756 default:
757 throw new IllegalArgumentException("Cannot interpret '" + hexDigit + "' as a hexadecimal digit");
758 }
759 }
760
761
762
763
764
765
766
767
768
769
770
771
772 public static int hexDigitToInt(final char hexDigit) {
773 final int digit = Character.digit(hexDigit, 16);
774 if (digit < 0) {
775 throw new IllegalArgumentException("Cannot interpret '" + hexDigit + "' as a hexadecimal digit");
776 }
777 return digit;
778 }
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793 public static byte hexToByte(final String src, final int srcPos, final byte dstInit, final int dstPos,
794 final int nHex) {
795 if (0 == nHex) {
796 return dstInit;
797 }
798 if ((nHex - 1) * 4 + dstPos >= 8) {
799 throw new IllegalArgumentException("(nHexs-1)*4+dstPos is greater or equal to than 8");
800 }
801 byte out = dstInit;
802 for (int i = 0; i < nHex; i++) {
803 final int shift = i * 4 + dstPos;
804 final int bits = (0xf & hexDigitToInt(src.charAt(i + srcPos))) << shift;
805 final int mask = 0xf << shift;
806 out = (byte) ((out & ~mask) | bits);
807 }
808 return out;
809 }
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824 public static int hexToInt(final String src, final int srcPos, final int dstInit, final int dstPos, final int nHex) {
825 if (0 == nHex) {
826 return dstInit;
827 }
828 if ((nHex - 1) * 4 + dstPos >= 32) {
829 throw new IllegalArgumentException("(nHexs-1)*4+dstPos is greater or equal to than 32");
830 }
831 int out = dstInit;
832 for (int i = 0; i < nHex; i++) {
833 final int shift = i * 4 + dstPos;
834 final int bits = (0xf & hexDigitToInt(src.charAt(i + srcPos))) << shift;
835 final int mask = 0xf << shift;
836 out = (out & ~mask) | bits;
837 }
838 return out;
839 }
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854 public static long hexToLong(final String src, final int srcPos, final long dstInit, final int dstPos,
855 final int nHex) {
856 if (0 == nHex) {
857 return dstInit;
858 }
859 if ((nHex - 1) * 4 + dstPos >= 64) {
860 throw new IllegalArgumentException("(nHexs-1)*4+dstPos is greater or equal to than 64");
861 }
862 long out = dstInit;
863 for (int i = 0; i < nHex; i++) {
864 final int shift = i * 4 + dstPos;
865 final long bits = (0xfL & hexDigitToInt(src.charAt(i + srcPos))) << shift;
866 final long mask = 0xfL << shift;
867 out = (out & ~mask) | bits;
868 }
869 return out;
870 }
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885 public static short hexToShort(final String src, final int srcPos, final short dstInit, final int dstPos,
886 final int nHex) {
887 if (0 == nHex) {
888 return dstInit;
889 }
890 if ((nHex - 1) * 4 + dstPos >= 16) {
891 throw new IllegalArgumentException("(nHexs-1)*4+dstPos is greater or equal to than 16");
892 }
893 short out = dstInit;
894 for (int i = 0; i < nHex; i++) {
895 final int shift = i * 4 + dstPos;
896 final int bits = (0xf & hexDigitToInt(src.charAt(i + srcPos))) << shift;
897 final int mask = 0xf << shift;
898 out = (short) ((out & ~mask) | bits);
899 }
900 return out;
901 }
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918 public static long intArrayToLong(final int[] src, final int srcPos, final long dstInit, final int dstPos,
919 final int nInts) {
920 if (src.length == 0 && srcPos == 0 || 0 == nInts) {
921 return dstInit;
922 }
923 if ((nInts - 1) * 32 + dstPos >= 64) {
924 throw new IllegalArgumentException("(nInts-1)*32+dstPos is greater or equal to than 64");
925 }
926 long out = dstInit;
927 for (int i = 0; i < nInts; i++) {
928 final int shift = i * 32 + dstPos;
929 final long bits = (0xffffffffL & src[i + srcPos]) << shift;
930 final long mask = 0xffffffffL << shift;
931 out = (out & ~mask) | bits;
932 }
933 return out;
934 }
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951 public static boolean[] intToBinary(final int src, final int srcPos, final boolean[] dst, final int dstPos,
952 final int nBools) {
953 if (0 == nBools) {
954 return dst;
955 }
956 if (nBools - 1 + srcPos >= 32) {
957 throw new IllegalArgumentException("nBools-1+srcPos is greater or equal to than 32");
958 }
959 for (int i = 0; i < nBools; i++) {
960 final int shift = i + srcPos;
961 dst[dstPos + i] = (0x1 & (src >> shift)) != 0;
962 }
963 return dst;
964 }
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981 public static byte[] intToByteArray(final int src, final int srcPos, final byte[] dst, final int dstPos,
982 final int nBytes) {
983 if (0 == nBytes) {
984 return dst;
985 }
986 if ((nBytes - 1) * 8 + srcPos >= 32) {
987 throw new IllegalArgumentException("(nBytes-1)*8+srcPos is greater or equal to than 32");
988 }
989 for (int i = 0; i < nBytes; i++) {
990 final int shift = i * 8 + srcPos;
991 dst[dstPos + i] = (byte) (0xff & (src >> shift));
992 }
993 return dst;
994 }
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010 public static String intToHex(final int src, final int srcPos, final String dstInit, final int dstPos,
1011 final int nHexs) {
1012 if (0 == nHexs) {
1013 return dstInit;
1014 }
1015 if ((nHexs - 1) * 4 + srcPos >= 32) {
1016 throw new IllegalArgumentException("(nHexs-1)*4+srcPos is greater or equal to than 32");
1017 }
1018 final StringBuilder sb = new StringBuilder(dstInit);
1019 int append = sb.length();
1020 for (int i = 0; i < nHexs; i++) {
1021 final int shift = i * 4 + srcPos;
1022 final int bits = 0xF & (src >> shift);
1023 if (dstPos + i == append) {
1024 ++append;
1025 sb.append(intToHexDigit(bits));
1026 } else {
1027 sb.setCharAt(dstPos + i, intToHexDigit(bits));
1028 }
1029 }
1030 return sb.toString();
1031 }
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050 public static char intToHexDigit(final int nibble) {
1051 final char c = Character.forDigit(nibble, 16);
1052 if (c == Character.MIN_VALUE) {
1053 throw new IllegalArgumentException("nibble value not between 0 and 15: " + nibble);
1054 }
1055 return c;
1056 }
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075 public static char intToHexDigitMsb0(final int nibble) {
1076 switch (nibble) {
1077 case 0x0:
1078 return '0';
1079 case 0x1:
1080 return '8';
1081 case 0x2:
1082 return '4';
1083 case 0x3:
1084 return 'c';
1085 case 0x4:
1086 return '2';
1087 case 0x5:
1088 return 'a';
1089 case 0x6:
1090 return '6';
1091 case 0x7:
1092 return 'e';
1093 case 0x8:
1094 return '1';
1095 case 0x9:
1096 return '9';
1097 case 0xA:
1098 return '5';
1099 case 0xB:
1100 return 'd';
1101 case 0xC:
1102 return '3';
1103 case 0xD:
1104 return 'b';
1105 case 0xE:
1106 return '7';
1107 case 0xF:
1108 return 'f';
1109 default:
1110 throw new IllegalArgumentException("nibble value not between 0 and 15: " + nibble);
1111 }
1112 }
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129 public static short[] intToShortArray(final int src, final int srcPos, final short[] dst, final int dstPos,
1130 final int nShorts) {
1131 if (0 == nShorts) {
1132 return dst;
1133 }
1134 if ((nShorts - 1) * 16 + srcPos >= 32) {
1135 throw new IllegalArgumentException("(nShorts-1)*16+srcPos is greater or equal to than 32");
1136 }
1137 for (int i = 0; i < nShorts; i++) {
1138 final int shift = i * 16 + srcPos;
1139 dst[dstPos + i] = (short) (0xffff & (src >> shift));
1140 }
1141 return dst;
1142 }
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159 public static boolean[] longToBinary(final long src, final int srcPos, final boolean[] dst, final int dstPos,
1160 final int nBools) {
1161 if (0 == nBools) {
1162 return dst;
1163 }
1164 if (nBools - 1 + srcPos >= 64) {
1165 throw new IllegalArgumentException("nBools-1+srcPos is greater or equal to than 64");
1166 }
1167 for (int i = 0; i < nBools; i++) {
1168 final int shift = i + srcPos;
1169 dst[dstPos + i] = (0x1 & (src >> shift)) != 0;
1170 }
1171 return dst;
1172 }
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189 public static byte[] longToByteArray(final long src, final int srcPos, final byte[] dst, final int dstPos,
1190 final int nBytes) {
1191 if (0 == nBytes) {
1192 return dst;
1193 }
1194 if ((nBytes - 1) * 8 + srcPos >= 64) {
1195 throw new IllegalArgumentException("(nBytes-1)*8+srcPos is greater or equal to than 64");
1196 }
1197 for (int i = 0; i < nBytes; i++) {
1198 final int shift = i * 8 + srcPos;
1199 dst[dstPos + i] = (byte) (0xff & (src >> shift));
1200 }
1201 return dst;
1202 }
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218 public static String longToHex(final long src, final int srcPos, final String dstInit, final int dstPos,
1219 final int nHexs) {
1220 if (0 == nHexs) {
1221 return dstInit;
1222 }
1223 if ((nHexs - 1) * 4 + srcPos >= 64) {
1224 throw new IllegalArgumentException("(nHexs-1)*4+srcPos is greater or equal to than 64");
1225 }
1226 final StringBuilder sb = new StringBuilder(dstInit);
1227 int append = sb.length();
1228 for (int i = 0; i < nHexs; i++) {
1229 final int shift = i * 4 + srcPos;
1230 final int bits = (int) (0xF & (src >> shift));
1231 if (dstPos + i == append) {
1232 ++append;
1233 sb.append(intToHexDigit(bits));
1234 } else {
1235 sb.setCharAt(dstPos + i, intToHexDigit(bits));
1236 }
1237 }
1238 return sb.toString();
1239 }
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256 public static int[] longToIntArray(final long src, final int srcPos, final int[] dst, final int dstPos,
1257 final int nInts) {
1258 if (0 == nInts) {
1259 return dst;
1260 }
1261 if ((nInts - 1) * 32 + srcPos >= 64) {
1262 throw new IllegalArgumentException("(nInts-1)*32+srcPos is greater or equal to than 64");
1263 }
1264 for (int i = 0; i < nInts; i++) {
1265 final int shift = i * 32 + srcPos;
1266 dst[dstPos + i] = (int) (0xffffffff & (src >> shift));
1267 }
1268 return dst;
1269 }
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286 public static short[] longToShortArray(final long src, final int srcPos, final short[] dst, final int dstPos,
1287 final int nShorts) {
1288 if (0 == nShorts) {
1289 return dst;
1290 }
1291 if ((nShorts - 1) * 16 + srcPos >= 64) {
1292 throw new IllegalArgumentException("(nShorts-1)*16+srcPos is greater or equal to than 64");
1293 }
1294 for (int i = 0; i < nShorts; i++) {
1295 final int shift = i * 16 + srcPos;
1296 dst[dstPos + i] = (short) (0xffff & (src >> shift));
1297 }
1298 return dst;
1299 }
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316 public static int shortArrayToInt(final short[] src, final int srcPos, final int dstInit, final int dstPos,
1317 final int nShorts) {
1318 if (src.length == 0 && srcPos == 0 || 0 == nShorts) {
1319 return dstInit;
1320 }
1321 if ((nShorts - 1) * 16 + dstPos >= 32) {
1322 throw new IllegalArgumentException("(nShorts-1)*16+dstPos is greater or equal to than 32");
1323 }
1324 int out = dstInit;
1325 for (int i = 0; i < nShorts; i++) {
1326 final int shift = i * 16 + dstPos;
1327 final int bits = (0xffff & src[i + srcPos]) << shift;
1328 final int mask = 0xffff << shift;
1329 out = (out & ~mask) | bits;
1330 }
1331 return out;
1332 }
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349 public static long shortArrayToLong(final short[] src, final int srcPos, final long dstInit, final int dstPos,
1350 final int nShorts) {
1351 if (src.length == 0 && srcPos == 0 || 0 == nShorts) {
1352 return dstInit;
1353 }
1354 if ((nShorts - 1) * 16 + dstPos >= 64) {
1355 throw new IllegalArgumentException("(nShorts-1)*16+dstPos is greater or equal to than 64");
1356 }
1357 long out = dstInit;
1358 for (int i = 0; i < nShorts; i++) {
1359 final int shift = i * 16 + dstPos;
1360 final long bits = (0xffffL & src[i + srcPos]) << shift;
1361 final long mask = 0xffffL << shift;
1362 out = (out & ~mask) | bits;
1363 }
1364 return out;
1365 }
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382 public static boolean[] shortToBinary(final short src, final int srcPos, final boolean[] dst, final int dstPos,
1383 final int nBools) {
1384 if (0 == nBools) {
1385 return dst;
1386 }
1387 if (nBools - 1 + srcPos >= 16) {
1388 throw new IllegalArgumentException("nBools-1+srcPos is greater or equal to than 16");
1389 }
1390 assert (nBools - 1) < 16 - srcPos;
1391 for (int i = 0; i < nBools; i++) {
1392 final int shift = i + srcPos;
1393 dst[dstPos + i] = (0x1 & (src >> shift)) != 0;
1394 }
1395 return dst;
1396 }
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413 public static byte[] shortToByteArray(final short src, final int srcPos, final byte[] dst, final int dstPos,
1414 final int nBytes) {
1415 if (0 == nBytes) {
1416 return dst;
1417 }
1418 if ((nBytes - 1) * 8 + srcPos >= 16) {
1419 throw new IllegalArgumentException("(nBytes-1)*8+srcPos is greater or equal to than 16");
1420 }
1421 for (int i = 0; i < nBytes; i++) {
1422 final int shift = i * 8 + srcPos;
1423 dst[dstPos + i] = (byte) (0xff & (src >> shift));
1424 }
1425 return dst;
1426 }
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442 public static String shortToHex(final short src, final int srcPos, final String dstInit, final int dstPos,
1443 final int nHexs) {
1444 if (0 == nHexs) {
1445 return dstInit;
1446 }
1447 if ((nHexs - 1) * 4 + srcPos >= 16) {
1448 throw new IllegalArgumentException("(nHexs-1)*4+srcPos is greater or equal to than 16");
1449 }
1450 final StringBuilder sb = new StringBuilder(dstInit);
1451 int append = sb.length();
1452 for (int i = 0; i < nHexs; i++) {
1453 final int shift = i * 4 + srcPos;
1454 final int bits = 0xF & (src >> shift);
1455 if (dstPos + i == append) {
1456 ++append;
1457 sb.append(intToHexDigit(bits));
1458 } else {
1459 sb.setCharAt(dstPos + i, intToHexDigit(bits));
1460 }
1461 }
1462 return sb.toString();
1463 }
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479 public static byte[] uuidToByteArray(final UUID src, final byte[] dst, final int dstPos, final int nBytes) {
1480 if (0 == nBytes) {
1481 return dst;
1482 }
1483 if (nBytes > 16) {
1484 throw new IllegalArgumentException("nBytes is greater than 16");
1485 }
1486 longToByteArray(src.getMostSignificantBits(), 0, dst, dstPos, Math.min(nBytes, 8));
1487 if (nBytes >= 8) {
1488 longToByteArray(src.getLeastSignificantBits(), 0, dst, dstPos + 8, nBytes - 8);
1489 }
1490 return dst;
1491 }
1492 }