Java Utililty Methods Byte Array to Endian

List of utility methods to do Byte Array to Endian

Description

The list of methods to do Byte Array to Endian are organized into topic(s).

Method

doubleBytesToDouble_With_Little_Endian(byte[] bytes)
Bytes To Doubl Wit Littl Endian
if (bytes == null || bytes.length != 8) {
    return 0;
long tmp = 0;
for (int i = 0; i < 8; i++) {
    tmp = (tmp << 8) | (bytes[i] & 0xFF);
return Double.longBitsToDouble(tmp);
...
intbytesToInt(byte[] bytes, int offset, int length, boolean littleEndian)
Converts a sequence of bytes with the specified byte order to an int .
return (littleEndian ? bytesToIntLE(bytes, offset, length) : bytesToIntBE(bytes, offset, length));
intbytesToInt32(byte[] buffer, int byteOffset, boolean bigEndian)
Converts a 4 successive bytes starting at byteOffset in buffer to a signed 32bit integer sample.
return bigEndian ? ((buffer[byteOffset] << 24) 
        | ((buffer[byteOffset + 1] & 0xFF) << 16) 
        | ((buffer[byteOffset + 2] & 0xFF) << 8) 
        | (buffer[byteOffset + 3] & 0xFF))
        : ((buffer[byteOffset + 3] << 24) 
                | ((buffer[byteOffset + 2] & 0xFF) << 16) 
                | ((buffer[byteOffset + 1] & 0xFF) << 8) 
                | (buffer[byteOffset] & 0xFF));
...
intbytesToIntBigEndian(byte[] buf)
byte array to int (big endian)
return ((buf[0] << 24) & 0xff000000) | ((buf[1] << 16) & 0xff0000) | ((buf[2] << 8) & 0xff00)
        | (buf[3] & 0xff);
intbytesToIntLE(byte[] bytes, int offset, int length)
Converts a little-endian sequence of bytes to an int .
int i = offset + length;
int value = bytes[--i];
while (--i >= offset) {
    value <<= 8;
    value |= bytes[i] & 0xFF;
return value;
longbytesToLongLittleEndian(final byte[] vals, final int from)
bytes into single long
final int bot = (vals[from + 3] << 24) + ((vals[from + 2] & 0xFF) << 16) + ((vals[from + 1] & 0xFF) << 8)
        + (vals[from] & 0xFF);
final int top = (vals[from + 7] << 24) + ((vals[from + 6] & 0xFF) << 16) + ((vals[from + 5] & 0xFF) << 8)
        + (vals[from + 4] & 0xFF);
return ((long) top << 32) + ((long) bot & 0xFFFFFFFFL);
longbytesToNumberLittleEndian(byte[] buffer, int start, int length)
Reconstructs a number that is represented by more than one byte in a network packet in little-endian order, for the very few protocol values that are sent in this quirky way.
long result = 0;
for (int index = start + length - 1; index >= start; index--) {
    result = (result << 8) + unsign(buffer[index]);
return result;
intbytesToTag(byte[] bytes, int off, boolean bigEndian)
bytes To Tag
return bigEndian ? bytesToTagBE(bytes, off) : bytesToTagLE(bytes, off);
longbytesToXBigEndian(byte[] in, int offset, int size)
bytes To X Big Endian
if (in == null) {
    throw new NullPointerException("in == null");
long res = 0;
for (int i = offset; i < (offset + size); i++) {
    res = (res << 8) | (in[i] & 0xff);
return res;
...