Skip to content

Commit ad06cb1

Browse files
committed
Encoder honor byte order
1 parent be961a4 commit ad06cb1

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ public Hex(boolean upperCase) {
7878
@Override
7979
public String encode(byte[] byteArray, ByteOrder byteOrder) {
8080
StringBuilder sb = new StringBuilder(byteArray.length * 2);
81-
for (byte anArray : byteArray) {
82-
char first4Bit = Character.forDigit((anArray >> 4) & 0xF, 16);
83-
char last4Bit = Character.forDigit((anArray & 0xF), 16);
81+
82+
int index;
83+
for (int i = 0; i < byteArray.length; i++) {
84+
index = (byteOrder == ByteOrder.BIG_ENDIAN) ? i : byteArray.length - i - 1;
85+
char first4Bit = Character.forDigit((byteArray[index] >> 4) & 0xF, 16);
86+
char last4Bit = Character.forDigit((byteArray[index] & 0xF), 16);
8487
if (upperCase) {
8588
first4Bit = Character.toUpperCase(first4Bit);
8689
last4Bit = Character.toUpperCase(last4Bit);
@@ -114,7 +117,7 @@ public byte[] decode(String hexString) {
114117
class Base64Encoding implements Encoder, Decoder {
115118
@Override
116119
public String encode(byte[] array, ByteOrder byteOrder) {
117-
return Base64.encode(array);
120+
return Base64.encode((byteOrder == ByteOrder.BIG_ENDIAN) ? array : Bytes.from(array).reverse().array());
118121
}
119122

120123
@Override
@@ -132,7 +135,7 @@ class BaseRadix implements Encoder, Decoder {
132135

133136
@Override
134137
public String encode(byte[] array, ByteOrder byteOrder) {
135-
return new BigInteger(1, array).toString(radix);
138+
return new BigInteger(1, (byteOrder == ByteOrder.BIG_ENDIAN) ? array : Bytes.from(array).reverse().array()).toString(radix);
136139
}
137140

138141
@Override
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package at.favre.lib.bytes;
2+
3+
import org.junit.Test;
4+
5+
import java.nio.ByteOrder;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNotEquals;
9+
10+
public class BinaryToTextEncodingTest {
11+
@Test
12+
public void encodeHex() throws Exception {
13+
assertEquals("010203", new BinaryToTextEncoding.Hex(false).encode(new byte[]{1, 2, 3}, ByteOrder.BIG_ENDIAN));
14+
assertEquals("030201", new BinaryToTextEncoding.Hex(false).encode(new byte[]{1, 2, 3}, ByteOrder.LITTLE_ENDIAN));
15+
assertNotEquals(new BinaryToTextEncoding.Hex(false).encode(new byte[]{1, 2, 3}, ByteOrder.LITTLE_ENDIAN), new BinaryToTextEncoding.Hex(false).encode(new byte[]{1, 2, 3}, ByteOrder.BIG_ENDIAN));
16+
}
17+
18+
@Test
19+
public void encodeBaseRadix() throws Exception {
20+
assertEquals("100211", new BinaryToTextEncoding.BaseRadix(16).encode(new byte[]{16, 2, 17}, ByteOrder.BIG_ENDIAN));
21+
assertEquals("110210", new BinaryToTextEncoding.BaseRadix(16).encode(new byte[]{16, 2, 17}, ByteOrder.LITTLE_ENDIAN));
22+
assertNotEquals(new BinaryToTextEncoding.BaseRadix(2).encode(new byte[]{1, 2, 3}, ByteOrder.LITTLE_ENDIAN), new BinaryToTextEncoding.BaseRadix(2).encode(new byte[]{1, 2, 3}, ByteOrder.BIG_ENDIAN));
23+
24+
}
25+
26+
@Test
27+
public void encodeBase64() throws Exception {
28+
assertEquals("EAIR", new BinaryToTextEncoding.Base64Encoding().encode(new byte[]{16, 2, 17}, ByteOrder.BIG_ENDIAN));
29+
assertEquals("EQIQ", new BinaryToTextEncoding.Base64Encoding().encode(new byte[]{17, 2, 16}, ByteOrder.BIG_ENDIAN));
30+
assertEquals("EQIQ", new BinaryToTextEncoding.Base64Encoding().encode(new byte[]{16, 2, 17}, ByteOrder.LITTLE_ENDIAN));
31+
assertNotEquals(new BinaryToTextEncoding.Base64Encoding().encode(new byte[]{1, 2, 3}, ByteOrder.LITTLE_ENDIAN), new BinaryToTextEncoding.Base64Encoding().encode(new byte[]{1, 2, 3}, ByteOrder.BIG_ENDIAN));
32+
}
33+
}

0 commit comments

Comments
 (0)