Java Utililty Methods Unsigned Number Create

List of utility methods to do Unsigned Number Create

Description

The list of methods to do Unsigned Number Create are organized into topic(s).

Method

intunsignedShortBytesToInt(byte[] b)
Converts an array of 2 bytes representing an unsigned short to an int containing the short value.
int i = b[1] & 0xFF;
i <<= 8;
i |= b[0] & 0xFF;
return i;
intunsignedShortToInt(byte[] b)
unsigned Short To Int
int i = 0;
i |= b[0] & 0xFF;
i <<= 8;
i |= b[1] & 0xFF;
return i;
intunsignedShortToInt(final byte[] b)
Converts a two byte array to an integer
int i = 0;
i |= b[0] & 0xFF;
i <<= 8;
i |= b[1] & 0xFF;
return i;
intunsignedShortToInt(short n)
Generate an integer from an unsigned short.
return n & 0x0000FFFF;
booleanunsignedSubOverflow(int operand1, int operand2)
Returns true, if the subtraction of both operand1 - operand2 (both unsigned) will issue a borrow.
operand1 += Integer.MIN_VALUE;
operand2 += Integer.MIN_VALUE;
return operand1 < operand2;
intunsignedToBytes(byte b)
unsigned To Bytes
return b & 0xFF;
intunsignedToSigned(int unsigned, int size)
unsigned To Signed
if ((unsigned & (1 << size - 1)) != 0)
    unsigned = -1 * ((1 << size - 1) - (unsigned & ((1 << size - 1) - 1)));
return unsigned;
byte[]unsignedToSigned(int[] ints)
Convert an unsigned set of bytes to a signed set.
final byte[] result = new byte[ints.length];
for (int i = 0; i < ints.length; i++) {
    result[i] = (byte) (ints[i] & 0xFF);
return result;
byte[]unsignedToSigned(int[] unsignedBytes)
unsigned To Signed
byte[] signedBytes = new byte[unsignedBytes.length];
for (int i = 0; i < unsignedBytes.length; i++) {
    if (i < 0 || i > 255) {
        throw new IllegalArgumentException(String.format(
                "Invalid value at position %d: %d is not an unsigned 8-bit integer (between 0 and 255)", i,
                unsignedBytes[i]));
    signedBytes[i] = (byte) (unsignedBytes[i]);
...
longunsignedToSigned(long value, int size)
unsigned To Signed
long signbit = value & (1L << (size - 1));
return (signbit == 0) ? value : (value - (1L << size));