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

bytegetMostSignificantByte(byte[] bytes, ByteOrder byteOrder)
Return the most significant byte in the provided byte array based on endianess.
byte mostSignificantByte = bytes[0];
if (byteOrder == ByteOrder.BIG_ENDIAN) {
} else if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
    mostSignificantByte = bytes[bytes.length - 1];
} else {
    throw new IllegalStateException("Unrecognized ByteOrder value[" + byteOrder + "].");
return mostSignificantByte;
...
intgetUnsignedShort(final int offset, final byte[] buffer, final ByteOrder byteOrder)
get Unsigned Short
if (byteOrder == ByteOrder.BIG_ENDIAN) {
    return (buffer[offset + 0] & 0xff) << 8 | buffer[offset + 1] & 0xff;
return (buffer[offset + 1] & 0xff) << 8 | buffer[offset + 0] & 0xff;
byte[]increaseNumberOfBytes(byte[] originalBytes, int desiredNumberOfBytes, ByteOrder byteOrder, boolean isSigned)
Increase the number of bytes used to store this number without affecting its value.
if (originalBytes.length >= desiredNumberOfBytes) {
    throw new IllegalArgumentException(
            "Cannot increase the number of bytes when the size of the original byte array["
                    + originalBytes.length + "] is greater than or equal to the desired number of bytes["
                    + desiredNumberOfBytes + "].");
int numberOfBytesToInsert = desiredNumberOfBytes - originalBytes.length;
byte[] newBytes = new byte[desiredNumberOfBytes];
...
booleanisNegative(byte[] bytes, ByteOrder byteOrder, boolean isSigned)
Detect whether this a negative value.
boolean isNegative = false;
if (isSigned) {
    byte mostSignificantByte = getMostSignificantByte(bytes, byteOrder);
    isNegative = isBitOn(mostSignificantByte, BITS_PER_BYTE - 1);
return isNegative;
byte[]longToBytes(long longValue, ByteOrder byteOrder, boolean isSigned)
Convert the provided longValue to a byte array of size 8.
byte[] result = new byte[BYTES_IN_A_LONG];
if (longValue < 0 && !isSigned) {
    throw new IllegalStateException(
            "Cannot represent the negative value[" + longValue + "] in an unsigned byte array.");
if (byteOrder == ByteOrder.BIG_ENDIAN) {
    for (int i = BYTES_IN_A_LONG - 1; i >= 0; i--) {
        result[i] = (byte) (longValue & 0xFF);
...
ByteOrderopposite(ByteOrder order)
opposite
return (order == ByteOrder.BIG_ENDIAN) ? ByteOrder.LITTLE_ENDIAN
        : (order != null) ? ByteOrder.BIG_ENDIAN : null;
byte[]reduceToSmallestByteArray(byte[] originalBytes, ByteOrder byteOrder, boolean isSigned)
Reduces the number of bytes to represent the provided number without changing its value.
byte[] smallestBytes = null;
byte emptyByte = (byte) 0;
if (isNegative(originalBytes, byteOrder, isSigned)) {
    emptyByte = (byte) -1;
if (byteOrder == ByteOrder.BIG_ENDIAN) {
    int byteIndexOfFirstValuedByte = 0;
    byteLoop: for (int i = 0; i < originalBytes.length; i++) {
...
byte[]setLong(byte[] bytes, int start, int end, long value, ByteOrder byteOrder)
set a long value in a byte array
for (int n = 0; n <= (end - start); n++) {
    byte b = (byte) (value % 256);
    bytes[ByteOrder.BIG_ENDIAN.equals(byteOrder) ? (end - start) - (n - start) : n + start] = b;
    value >>= 8;
return bytes;
byte[]toBytes(int value, ByteOrder order)
to Bytes
byte[] result = new byte[COUNT];
if (ByteOrder.BIG_ENDIAN.equals(order)) {
    int i = COUNT - 1;
    do {
        result[i] = (byte) (value % 256);
        value = (int) (value / 256);
        --i;
    } while (i >= 0);
...
inttoDoubleByteArray(double val, ByteOrder outOrder, byte[] buf, int off)
to Double Byte Array
return toInt64ByteArray(Double.doubleToLongBits(val), outOrder, buf, off);