short
which should be treated as unsigned to an
* int
having a range of [0, 65535].
*/
public static int unsignedShortToInt(final short b) {
return b & 0xffff;
}
/**
* Converts the given two bytes
s to a short
.
*/
public static short bytesToShort(final byte highByte, final byte lowByte) {
final short high = (short) (highByte & 0xff);
final short low = (short) (lowByte & 0xff);
return (short) (high << 8 | low);
}
/**
* Converts the given int
to an unsigned byte
by
* ensuring it is within the range of [0, 255] using the following rules:
* int
is already within the range [0, 255],
* then it is simply cast to a byte
and returned.int
is negative, this method returns a
* byte
of 0.int
is greater than 255, this method
* returns a byte
of 255 (i.e. the value 255 cast to a byte).
* Converts the bytes specified by offset
and
* length
in the given array of bytes containing ASCII hex
* values to an array of bytes. For example, if the given array was created
* like this:
*
* final byte[] asciiHexBytes = new byte[]{48, 48, 48, 48, 51, 51, 48, 48, 48, 70, 48, 48, 48, 48, 50, 51, 48, 48, 48, 48, 48, 49, 70, 70};
*
* * ...or, equivalently, like this... *
*
* final byte[] asciiHexBytes = "000033000F000023000001FF".getBytes();
*
*
* ...then this method would convert the hex values (
* 00 00 33 00 0F 00 00 23 00 00 01 FF
) to bytes and return the
* a byte array equal to the following:
*
* final byte[] byteArray4 = new byte[]{0,0,51,0,15,0,0,35,0,0,1,-1}
*
*
* Returns null
if the given array is null
.
*
length
is negative or odd
* @throws ArrayIndexOutOfBoundsException
* if the offset
is negative or out of bounds, or
* if the sum of the offset
and the
* length
is greater than the length of the given
* array
*/
public static byte[] asciiHexBytesToByteArray(final byte[] asciiHexBytes,
final int offset, final int length) {
if (asciiHexBytes != null) {
if (offset < 0) {
throw new ArrayIndexOutOfBoundsException(
"Offset cannot be negative.");
}
if (offset >= asciiHexBytes.length) {
throw new ArrayIndexOutOfBoundsException(
"Offset is out of bounds.");
}
if (length < 0) {
throw new IllegalArgumentException("Length cannot be negative.");
}
if (length % 2 != 0) {
throw new IllegalArgumentException("Length must be even.");
}
if (offset + length > asciiHexBytes.length) {
throw new ArrayIndexOutOfBoundsException(
"Specified length is too long, not enough elements.");
}
// do the conversion (code based on
// http://mindprod.com/jgloss/hex.html)
final byte[] output = new byte[length / 2];
for (int i = offset, j = 0; i < offset + length; i += 2, j++) {
final int high = charToNibble((char) asciiHexBytes[i]);
final int low = charToNibble((char) asciiHexBytes[i + 1]);
output[j] = (byte) ((high << 4) | low);
}
return output;
}
return null;
}
/**
* * Converts the given array of bytes containing ASCII hex values to an array * of bytes. For example, if the given array was created like this: *
*
* final byte[] asciiHexBytes = new byte[]{48, 48, 48, 48, 51, 51, 48, 48, 48, 70, 48, 48, 48, 48, 50, 51, 48, 48, 48, 48, 48, 49, 70, 70};
*
* * ...or, equivalently, like this... *
*
* final byte[] asciiHexBytes = "000033000F000023000001FF".getBytes();
*
*
* ...then this method would convert the hex values (
* 00 00 33 00 0F 00 00 23 00 00 01 FF
) to bytes and return the
* a byte array equal to the following:
*
* final byte[] byteArray4 = new byte[]{0,0,51,0,15,0,0,35,0,0,1,-1}
*
*
* Returns null
if the given array is null
.
*