Skip to content

Commit 743a6ab

Browse files
committed
Fix radix encoder adding extra byte
1 parent 3bab2c1 commit 743a6ab

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/main/java/at/favre/lib/bytes/BinaryToTextEncoding.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,23 @@ class BaseRadix implements EncoderDecoder {
155155

156156
@Override
157157
public String encode(byte[] array, ByteOrder byteOrder) {
158+
System.out.println("max length " + maxLength(array, radix));
158159
return new BigInteger(1, (byteOrder == ByteOrder.BIG_ENDIAN) ? array : Bytes.from(array).reverse().array()).toString(radix);
159160
}
160161

161162
@Override
162163
public byte[] decode(String encoded) {
163-
return new BigInteger(encoded, radix).toByteArray();
164+
byte[] array = new BigInteger(encoded, radix).toByteArray();
165+
if (array[0] == 0) {
166+
byte[] tmp = new byte[array.length - 1];
167+
System.arraycopy(array, 1, tmp, 0, tmp.length);
168+
array = tmp;
169+
}
170+
return array;
171+
}
172+
173+
private int maxLength(byte[] data, int radix) {
174+
return BigInteger.valueOf(2).pow(BigInteger.valueOf(data.length).multiply(BigInteger.valueOf(8)).intValue()).toString(radix).length();
164175
}
165176
}
166177
}

src/test/java/at/favre/lib/bytes/BinaryToTextEncodingTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626

2727
import java.nio.ByteOrder;
2828

29-
import static org.junit.Assert.assertEquals;
30-
import static org.junit.Assert.assertNotEquals;
29+
import static org.junit.Assert.*;
3130

3231
public class BinaryToTextEncodingTest {
3332
@Test
@@ -50,7 +49,6 @@ public void encodeBaseRadix() throws Exception {
5049
}
5150

5251
@Test
53-
@Ignore
5452
public void encodeDecodeRadix() throws Exception {
5553
for (int i = 0; i < 32; i++) {
5654
Bytes rnd = Bytes.random(i);
@@ -62,10 +60,25 @@ public void encodeDecodeRadix() throws Exception {
6260
System.out.println("radix" + j + ":\t" + encodedBigEndian);
6361
System.out.println("orig :\t" + rnd.encodeHex());
6462
System.out.println("enc :\t" + Bytes.wrap(decoded).encodeHex());
63+
assertArrayEquals(rnd.array(), decoded);
6564
}
6665
}
6766
}
6867

68+
@Test
69+
@Ignore("should fix")
70+
public void encodeDecodeRadixZeros() throws Exception {
71+
Bytes bytes = Bytes.wrap(new byte[]{0, 0, 0, 0});
72+
BinaryToTextEncoding.EncoderDecoder encoding = new BinaryToTextEncoding.BaseRadix(36);
73+
String encodedBigEndian = encoding.encode(bytes.array(), ByteOrder.BIG_ENDIAN);
74+
byte[] decoded = encoding.decode(encodedBigEndian);
75+
76+
System.out.println("radix36:\t" + encodedBigEndian);
77+
System.out.println("orig :\t" + bytes.encodeHex());
78+
System.out.println("enc :\t" + Bytes.wrap(decoded).encodeHex());
79+
assertArrayEquals(bytes.array(), decoded);
80+
}
81+
6982
@Test
7083
public void encodeDecodeBase64() throws Exception {
7184
for (int i = 4; i < 32; i += 4) {

0 commit comments

Comments
 (0)