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