bug fix, fixed reverse order in BinaryStruct parsing

This commit is contained in:
Ziver Koc 2016-02-02 09:38:08 +01:00
parent 644c5ab6ea
commit 4d31e66ebd
3 changed files with 23 additions and 8 deletions

View file

@ -317,10 +317,24 @@ public class Converter {
int i = 0;
switch (b.length){
default:
case 4: i |= 0xFF000000 & (b[3] << 24);
case 3: i |= 0x00FF0000 & (b[2] << 16);
case 2: i |= 0x0000FF00 & (b[1] << 8);
case 1: i |= 0x000000FF & b[0]; break;
case 4:
i |= 0xFF000000 & (b[0] << 24);
i |= 0x00FF0000 & (b[1] << 16);
i |= 0x0000FF00 & (b[2] << 8);
i |= 0x000000FF & b[3];
break;
case 3:
i |= 0x00FF0000 & (b[0] << 16);
i |= 0x0000FF00 & (b[1] << 8);
i |= 0x000000FF & b[2];
break;
case 2:
i |= 0x0000FF00 & (b[0] << 8);
i |= 0x000000FF & b[1];
break;
case 1:
i |= 0x000000FF & b[0];
break;
case 0: break;
}
return i;

View file

@ -56,7 +56,7 @@ public class BinaryStructParser {
int readLength = 0;
byte[] valueData = new byte[(int) Math.ceil(length / 8.0)];
for (int index = valueData.length - 1; index >= 0; --index) {
for (int index = 0; index < valueData.length; ++index) {
valueData[index] = ByteUtil.getBits(data[byteIndex], bitIndex, bitLength);
readLength += bitLength;
byteIndex++;

View file

@ -82,9 +82,10 @@ public class ConverterTest {
public void byteArrayToInt(){
assertEquals(0, Converter.toInt(new byte[]{}));
assertEquals(1, Converter.toInt(new byte[]{0b0000_0001}));
assertEquals(1, Converter.toInt(new byte[]{0x01,0x00}));
assertEquals(1, Converter.toInt(new byte[]{0x00,0x01}));
assertEquals(256, Converter.toInt(new byte[]{0x00,0x01,0x00}));
assertEquals(Integer.MAX_VALUE, Converter.toInt(new byte[]{(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0x7F}));
assertEquals(Integer.MAX_VALUE, Converter.toInt(new byte[]{(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0x7F,(byte)0xFF,(byte)0xFF}));
assertEquals(-1, Converter.toInt(new byte[]{(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF}));
assertEquals(Integer.MAX_VALUE, Converter.toInt(new byte[]{(byte)0x7F,(byte)0xFF,(byte)0xFF,(byte)0xFF}));
assertEquals(Integer.MAX_VALUE, Converter.toInt(new byte[]{(byte)0x7F,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF}));
}
}