1+ package jota .utils ;
2+
3+ import java .util .Arrays ;
4+
5+ public class Converter {
6+
7+ public static final int RADIX = 3 ;
8+ public static final int MAX_TRIT_VALUE = (RADIX - 1 ) / 2 , MIN_TRIT_VALUE = -MAX_TRIT_VALUE ;
9+
10+ public static final int NUMBER_OF_TRITS_IN_A_BYTE = 5 ;
11+ public static final int NUMBER_OF_TRITS_IN_A_TRYTE = 3 ;
12+
13+ static final int [][] BYTE_TO_TRITS_MAPPINGS = new int [243 ][];
14+ static final int [][] TRYTE_TO_TRITS_MAPPINGS = new int [27 ][];
15+
16+ public static final String TRYTE_ALPHABET = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
17+
18+ static {
19+
20+ final int [] trits = new int [NUMBER_OF_TRITS_IN_A_BYTE ];
21+
22+ for (int i = 0 ; i < 243 ; i ++) {
23+ BYTE_TO_TRITS_MAPPINGS [i ] = Arrays .copyOf (trits , NUMBER_OF_TRITS_IN_A_BYTE );
24+ increment (trits , NUMBER_OF_TRITS_IN_A_BYTE );
25+ }
26+
27+ for (int i = 0 ; i < 27 ; i ++) {
28+ TRYTE_TO_TRITS_MAPPINGS [i ] = Arrays .copyOf (trits , NUMBER_OF_TRITS_IN_A_TRYTE );
29+ increment (trits , NUMBER_OF_TRITS_IN_A_TRYTE );
30+ }
31+ }
32+
33+ public static byte [] bytes (final int [] trits , final int offset , final int size ) {
34+
35+ final byte [] bytes = new byte [(size + NUMBER_OF_TRITS_IN_A_BYTE - 1 ) / NUMBER_OF_TRITS_IN_A_BYTE ];
36+ for (int i = 0 ; i < bytes .length ; i ++) {
37+
38+ int value = 0 ;
39+ for (int j = (size - i * NUMBER_OF_TRITS_IN_A_BYTE ) < 5 ? (size - i * NUMBER_OF_TRITS_IN_A_BYTE ) : NUMBER_OF_TRITS_IN_A_BYTE ; j -- > 0 ; ) {
40+ value = value * RADIX + trits [offset + i * NUMBER_OF_TRITS_IN_A_BYTE + j ];
41+ }
42+ bytes [i ] = (byte )value ;
43+ }
44+
45+ return bytes ;
46+ }
47+
48+ public static byte [] bytes (final int [] trits ) {
49+ return bytes (trits , 0 , trits .length );
50+ }
51+
52+ public static void getTrits (final byte [] bytes , final int [] trits ) {
53+
54+ int offset = 0 ;
55+ for (int i = 0 ; i < bytes .length && offset < trits .length ; i ++) {
56+ System .arraycopy (BYTE_TO_TRITS_MAPPINGS [bytes [i ] < 0 ? (bytes [i ] + BYTE_TO_TRITS_MAPPINGS .length ) : bytes [i ]], 0 , trits , offset , trits .length - offset < NUMBER_OF_TRITS_IN_A_BYTE ? (trits .length - offset ) : NUMBER_OF_TRITS_IN_A_BYTE );
57+ offset += NUMBER_OF_TRITS_IN_A_BYTE ;
58+ }
59+ while (offset < trits .length ) {
60+ trits [offset ++] = 0 ;
61+ }
62+ }
63+
64+ public static int [] trits (final String trytes ) {
65+
66+ final int [] trits = new int [trytes .length () * NUMBER_OF_TRITS_IN_A_TRYTE ];
67+ for (int i = 0 ; i < trytes .length (); i ++) {
68+ System .arraycopy (TRYTE_TO_TRITS_MAPPINGS [TRYTE_ALPHABET .indexOf (trytes .charAt (i ))], 0 , trits , i * NUMBER_OF_TRITS_IN_A_TRYTE , NUMBER_OF_TRITS_IN_A_TRYTE );
69+ }
70+
71+ return trits ;
72+ }
73+
74+ public static void copyTrits (final long value , final int [] destination , final int offset , final int size ) {
75+
76+ long absoluteValue = value < 0 ? -value : value ;
77+ for (int i = 0 ; i < size ; i ++) {
78+
79+ int remainder = (int )(absoluteValue % RADIX );
80+ absoluteValue /= RADIX ;
81+ if (remainder > MAX_TRIT_VALUE ) {
82+
83+ remainder = MIN_TRIT_VALUE ;
84+ absoluteValue ++;
85+ }
86+ destination [offset + i ] = remainder ;
87+ }
88+
89+ if (value < 0 ) {
90+
91+ for (int i = 0 ; i < size ; i ++) {
92+ destination [offset + i ] = -destination [offset + i ];
93+ }
94+ }
95+ }
96+
97+ public static String trytes (final int [] trits , final int offset , final int size ) {
98+
99+ StringBuilder trytes = new StringBuilder ();
100+ for (int i = 0 ; i < (size + NUMBER_OF_TRITS_IN_A_TRYTE - 1 ) / NUMBER_OF_TRITS_IN_A_TRYTE ; i ++) {
101+
102+ int j = trits [offset + i * 3 ] + trits [offset + i * 3 + 1 ] * 3 + trits [offset + i * 3 + 2 ] * 9 ;
103+ if (j < 0 ) {
104+
105+ j += TRYTE_ALPHABET .length ();
106+ }
107+ trytes .append (TRYTE_ALPHABET .charAt (j ));
108+ }
109+ return trytes .toString ();
110+ }
111+
112+ public static String trytes (final int [] trits ) {
113+ return trytes (trits , 0 , trits .length );
114+ }
115+
116+ public static int tryteValue (final int [] trits , final int offset ) {
117+ return trits [offset ] + trits [offset + 1 ] * 3 + trits [offset + 2 ] * 9 ;
118+ }
119+
120+ public static void increment (final int [] trits , final int size ) {
121+
122+ for (int i = 0 ; i < size ; i ++) {
123+ if (++trits [i ] > Converter .MAX_TRIT_VALUE ) {
124+ trits [i ] = Converter .MIN_TRIT_VALUE ;
125+ } else {
126+ break ;
127+ }
128+ }
129+ }
130+ }
0 commit comments