Java Utililty Methods ByteOrder

List of utility methods to do ByteOrder

Description

The list of methods to do ByteOrder are organized into topic(s).

Method

intbyteArrayToNumber(byte[] data, int index, int length, ByteOrder byteOrder)
byte Array To Number
int value;
    length = Math.min(length, data.length - index);
    value = 0;
    for (int i = index, endIndex = index + length; i < endIndex; i++) {
        value |= (data[i] & 0xff) << 8
                * (byteOrder == ByteOrder.LITTLE_ENDIAN ? i - index : endIndex - i - 1);
return value;
ByteOrdercomplementingOrder(ByteOrder order)
complementing Order
if (order == null) {
    return null;
} else if (ByteOrder.BIG_ENDIAN.equals(order)) {
    return ByteOrder.LITTLE_ENDIAN;
} else {
    return ByteOrder.BIG_ENDIAN;
BigIntegerconvertBytesToBigInteger(byte[] bytes, ByteOrder byteOrder, boolean isSigned)
convert Bytes To Big Integer
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
    bytes = toggleEndianess(bytes);
BigInteger value = new BigInteger(bytes);
int numberOfBytes = bytes.length;
boolean valueIsLessThanZero = value.compareTo(BigInteger.ZERO) < 0;
if (!isSigned && valueIsLessThanZero) {
    BigInteger unsignedEquivalent = value.add(BigInteger.valueOf(2).pow(BITS_PER_BYTE * numberOfBytes));
...
intcreateWord(final ByteOrder aByteOrder, final int aMSB, final int aLSB)
Creates a (16-bit) word value with the correct byte order.
if (aByteOrder == ByteOrder.BIG_ENDIAN) {
    return ((aMSB << 8) & 0xFF00) | (aLSB & 0x00FF);
return ((aLSB << 8) & 0xFF00) | (aMSB & 0x00FF);
longdecode(final ByteOrder aExpectedByteOrder, final byte... aBytes)
Convenience method to create a single value using the given byte values in a given byte order.
final int byteCount = aBytes.length;
final int lastByteIdx = byteCount - 1;
long result = 0L;
if (aExpectedByteOrder == ByteOrder.BIG_ENDIAN) {
    for (int i = 0; i < byteCount; i++) {
        result <<= 8;
        result |= (aBytes[i] & 0xFF);
} else if (aExpectedByteOrder == ByteOrder.LITTLE_ENDIAN) {
    for (int i = lastByteIdx; i >= 0; i--) {
        result <<= 8;
        result |= (aBytes[i] & 0xFF);
return result;
intget24BitInt(byte b1, byte b2, byte b3, ByteOrder order)
Returns an integer for the given 3 bytes.
if (order == ByteOrder.BIG_ENDIAN) {
    return ((b1 << 16) | ((b2 & 0xFF) << 8) | (b3 & 0xFF));
} else {
    return ((b3 << 16) | ((b2 & 0xFF) << 8) | (b1 & 0xFF));
ByteOrdergetByteOrder(final String value)
get Byte Order
if (value.equalsIgnoreCase(ByteOrder.LITTLE_ENDIAN.toString())) {
    return ByteOrder.LITTLE_ENDIAN;
} else if (value.equalsIgnoreCase(ByteOrder.BIG_ENDIAN.toString())) {
    return ByteOrder.BIG_ENDIAN;
return ByteOrder.BIG_ENDIAN;
intgetInt(byte[] b, int start, int end, ByteOrder byteOrder)
get an int value from a byte array
return (int) getLong(b, start, end, byteOrder);
intgetInt(final int offset, final byte[] buffer, final ByteOrder byteOrder)
get Int
return (int) getUnsignedInt(offset, buffer, byteOrder);
longgetLong(byte[] b, int start, int end, ByteOrder byteOrder)
get Long
long i = 0;
for (int n = 0; n <= (end - start); n++) {
    i <<= 8;
    i += ByteOrder.BIG_ENDIAN.equals(byteOrder) ? (b[n + start] & 0xff)
            : (b[(end - start) - (n - start)] & 0xff);
return i;