implemented variable length field
This commit is contained in:
parent
6e49a343f9
commit
227f294ae0
3 changed files with 62 additions and 40 deletions
|
|
@ -43,36 +43,44 @@ public class ByteUtil {
|
|||
};
|
||||
|
||||
/**
|
||||
* Creates a new subbyte from offset and with a length and shifts the data to the left
|
||||
* Creates a new sub byte from index and with a length and shifts the data to the left
|
||||
*
|
||||
* @param data is the byte data
|
||||
* @param offset is the bit index, valid values 0-7
|
||||
* @param index is the bit index, valid values 0-7
|
||||
* @param length is the length of bits to return, valid values 1-8
|
||||
* @return a new byte containing a subbyte defined by the offset and length
|
||||
* @return a new byte containing a sub byte defined by the index and length
|
||||
*/
|
||||
public static byte getShiftedBits(byte data, int offset, int length){
|
||||
int ret = 0xFF & getBits(data, offset, length);
|
||||
ret = ret >>> offset+1-length;
|
||||
public static byte getShiftedBits(byte data, int index, int length){
|
||||
int ret = 0xFF & getBits(data, index, length);
|
||||
ret = ret >>> index+1-length;
|
||||
return (byte) ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new subbyte from offset and with a length
|
||||
* Creates a new sub byte from index and with a length
|
||||
*
|
||||
* @param data is the byte data
|
||||
* @param offset is the bit index, valid values 0-7
|
||||
* @param index is the bit index, valid values 0-7
|
||||
* @param length is the length of bits to return, valid values 1-8
|
||||
* @return a new byte containing a subbyte defined by the offset and length
|
||||
* @return a new byte containing a sub byte defined by the index and length
|
||||
*/
|
||||
public static byte getBits(byte data, int offset, int length){
|
||||
public static byte getBits(byte data, int index, int length){
|
||||
length--;
|
||||
if(0 > offset || offset > 7)
|
||||
throw new IllegalArgumentException("Invalid index argument, allowed value is 0-7");
|
||||
if(length < 0 && offset-length < 0)
|
||||
throw new IllegalArgumentException("Invalid length argument: "+length+", allowed values 1-8 depending on index");
|
||||
|
||||
byte ret = (byte) (data & BYTE_MASK[offset][length]);
|
||||
byte ret = (byte) (data & getBitMask(index, length));
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a byte bitmask
|
||||
*
|
||||
* @param index start index of the mask, valid values 0-7
|
||||
* @param length length of mask from index, valid values 1-8
|
||||
*/
|
||||
public static byte getBitMask(int index, int length) {
|
||||
if(0 > index || index > 7)
|
||||
throw new IllegalArgumentException("Invalid index argument, allowed value is 0-7");
|
||||
if(length < 0 && index-length < 0)
|
||||
throw new IllegalArgumentException("Invalid length argument: "+length+", allowed values 1-8 depending on index");
|
||||
return (byte) BYTE_MASK[index][length];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue