Fixed some bugs in byte util
This commit is contained in:
parent
28d1f04246
commit
50ea517cf3
4 changed files with 9 additions and 7 deletions
|
|
@ -144,7 +144,7 @@ public class ByteUtil {
|
||||||
byte rest = 0;
|
byte rest = 0;
|
||||||
for (int i=0; i<data.length; ++i){
|
for (int i=0; i<data.length; ++i){
|
||||||
rest = (byte)(getBits(data[i], shiftBy-1, shiftBy) << 8 - shiftBy);
|
rest = (byte)(getBits(data[i], shiftBy-1, shiftBy) << 8 - shiftBy);
|
||||||
data[i] >>>= shiftBy;
|
data[i] = (byte)((int)(data[i]&0xFF) >> shiftBy);
|
||||||
if(i != 0)
|
if(i != 0)
|
||||||
data[i-1] |= rest;
|
data[i-1] |= rest;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -101,11 +101,11 @@ public class Converter {
|
||||||
if(hex.startsWith("0x"))
|
if(hex.startsWith("0x"))
|
||||||
hex = hex.substring(2);
|
hex = hex.substring(2);
|
||||||
byte[] b = new byte[(int)Math.ceil(hex.length()/2.0)];
|
byte[] b = new byte[(int)Math.ceil(hex.length()/2.0)];
|
||||||
for(int i=hex.length()-1; i>=0; i-=2){
|
for(int i=0; i<hex.length(); i+=2){
|
||||||
if(i-1 < 0)
|
if(i+1 >= hex.length())
|
||||||
b[(hex.length()-i-1)/2] = hexToByte(hex.charAt(i));
|
b[(i+1)/2] = hexToByte(hex.charAt(i), '0');
|
||||||
else
|
else
|
||||||
b[(hex.length()-i-1)/2] = hexToByte(hex.charAt(i-1), hex.charAt(i));
|
b[(i+1)/2] = hexToByte(hex.charAt(i), hex.charAt(i+1));
|
||||||
}
|
}
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -109,6 +109,8 @@ public class ByteUtilTest {
|
||||||
ByteUtil.shiftLeft(new byte[]{0b0000_0001}, 0));
|
ByteUtil.shiftLeft(new byte[]{0b0000_0001}, 0));
|
||||||
assertArrayEquals( new byte[]{0b0000_0001},
|
assertArrayEquals( new byte[]{0b0000_0001},
|
||||||
ByteUtil.shiftLeft(new byte[]{0b0001_0000}, 4));
|
ByteUtil.shiftLeft(new byte[]{0b0001_0000}, 4));
|
||||||
|
assertArrayEquals( new byte[]{0b0000_1000},
|
||||||
|
ByteUtil.shiftLeft(new byte[]{(byte)0b1000_0000}, 4));
|
||||||
assertArrayEquals( new byte[]{0b0001_0001, 0b0000_0000},
|
assertArrayEquals( new byte[]{0b0001_0001, 0b0000_0000},
|
||||||
ByteUtil.shiftLeft(new byte[]{0b0001_0000, 0b0000_0001}, 4));
|
ByteUtil.shiftLeft(new byte[]{0b0001_0000, 0b0000_0001}, 4));
|
||||||
assertArrayEquals( new byte[]{0b0100_1001, 0b0000_0001},
|
assertArrayEquals( new byte[]{0b0100_1001, 0b0000_0001},
|
||||||
|
|
|
||||||
|
|
@ -57,9 +57,9 @@ public class ConverterTest {
|
||||||
assertArrayEquals( new byte[]{}, Converter.hexToByte("") );
|
assertArrayEquals( new byte[]{}, Converter.hexToByte("") );
|
||||||
assertArrayEquals( new byte[]{0x00}, Converter.hexToByte("0x00") );
|
assertArrayEquals( new byte[]{0x00}, Converter.hexToByte("0x00") );
|
||||||
assertArrayEquals( new byte[]{0x00}, Converter.hexToByte("00") );
|
assertArrayEquals( new byte[]{0x00}, Converter.hexToByte("00") );
|
||||||
assertArrayEquals(new byte[]{0x07,0x06,0x05,0x04,0x03,0x02,0x01},
|
assertArrayEquals(new byte[]{0x01,0x02,0x03,0x04,0x05,0x06,0x07},
|
||||||
Converter.hexToByte("01020304050607") );
|
Converter.hexToByte("01020304050607") );
|
||||||
assertArrayEquals( new byte[]{0x11,0x0F}, Converter.hexToByte("F11") );
|
assertArrayEquals( new byte[]{(byte)0xF1,0x10}, Converter.hexToByte("F11") );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue