|
2 | 2 |
|
3 | 3 | import jota.model.Transaction; |
4 | 4 | import jota.pow.Curl; |
5 | | - |
6 | | -import java.util.Arrays; |
7 | | -import java.util.Optional; |
8 | | - |
9 | 5 | import org.apache.commons.lang3.StringUtils; |
10 | 6 | import org.slf4j.Logger; |
11 | 7 | import org.slf4j.LoggerFactory; |
12 | 8 |
|
| 9 | +import java.util.Arrays; |
| 10 | + |
13 | 11 | public class Converter { |
14 | 12 |
|
15 | 13 | private static final Logger log = LoggerFactory.getLogger(Converter.class); |
@@ -69,12 +67,39 @@ public static void getTrits(final byte[] bytes, final int[] trits) { |
69 | 67 | } |
70 | 68 |
|
71 | 69 | public static int[] trits(final String trytes) { |
72 | | - |
73 | 70 | final int[] trits = new int[trytes.length() * NUMBER_OF_TRITS_IN_A_TRYTE]; |
74 | | - for (int i = 0; i < trytes.length(); i++) { |
75 | | - System.arraycopy(TRYTE_TO_TRITS_MAPPINGS[Constants.TRYTE_ALPHABET.indexOf(trytes.charAt(i))], 0, trits, i * NUMBER_OF_TRITS_IN_A_TRYTE, NUMBER_OF_TRITS_IN_A_TRYTE); |
76 | | - } |
77 | 71 |
|
| 72 | + if (InputValidator.isValue(trytes)) { |
| 73 | + |
| 74 | + int value = Integer.parseInt(trytes); |
| 75 | + |
| 76 | + long absoluteValue = value < 0 ? -value : value; |
| 77 | + |
| 78 | + while (absoluteValue > 0) { |
| 79 | + |
| 80 | + int remainder = (int) (absoluteValue % RADIX); |
| 81 | + absoluteValue /= RADIX; |
| 82 | + |
| 83 | + if (remainder > MAX_TRIT_VALUE) { |
| 84 | + remainder = MIN_TRIT_VALUE; |
| 85 | + absoluteValue++; |
| 86 | + } |
| 87 | + |
| 88 | + trits[trits.length] = remainder; |
| 89 | + } |
| 90 | + if (value < 0) { |
| 91 | + |
| 92 | + for (int i = 0; i < trits.length; i++) { |
| 93 | + |
| 94 | + trits[i] = -trits[i]; |
| 95 | + } |
| 96 | + } |
| 97 | + } else { |
| 98 | + |
| 99 | + for (int i = 0; i < trytes.length(); i++) { |
| 100 | + System.arraycopy(TRYTE_TO_TRITS_MAPPINGS[Constants.TRYTE_ALPHABET.indexOf(trytes.charAt(i))], 0, trits, i * NUMBER_OF_TRITS_IN_A_TRYTE, NUMBER_OF_TRITS_IN_A_TRYTE); |
| 101 | + } |
| 102 | + } |
78 | 103 | return trits; |
79 | 104 | } |
80 | 105 |
|
|
0 commit comments