Java Byte Array to Endian bytesToInt(byte[] bytes, int offset, int length, boolean littleEndian)

Here you can find the source of bytesToInt(byte[] bytes, int offset, int length, boolean littleEndian)

Description

Converts a sequence of bytes with the specified byte order to an int .

License

Open Source License

Parameter

Parameter Description
bytes the byte sequence that is to be converted.
offset the offset to bytes at which the sequence starts.
length the length of the byte sequence.
littleEndian true if the byte order of the byte sequence is little-endian; false if the byte order is big-endian.

Return

the int that results from the conversion of the byte sequence.

Declaration


protected static int bytesToInt(byte[] bytes, int offset, int length, boolean littleEndian) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*w ww  .j  a v a2s.c om*/
     * Converts a sequence of bytes with the specified byte order to an {@code int}.
     *
     * @param  bytes         the byte sequence that is to be converted.
     * @param  offset        the offset to {@code bytes} at which the sequence starts.
     * @param  length        the length of the byte sequence.
     * @param  littleEndian  {@code true} if the byte order of the byte sequence is little-endian; {@code
     *                       false} if the byte order is big-endian.
     * @return the {@code int} that results from the conversion of the byte sequence.
     * @since  1.0
     * @see    #intToBytes(int, byte[], int, int, boolean)
     * @see    #bytesToLong(byte[], int, int, boolean)
     */

    protected static int bytesToInt(byte[] bytes, int offset, int length, boolean littleEndian) {
        return (littleEndian ? bytesToIntLE(bytes, offset, length) : bytesToIntBE(bytes, offset, length));
    }

    /**
     * Converts a little-endian sequence of bytes to an {@code int}.
     *
     * @param  bytes   the byte sequence that is to be converted.
     * @param  offset  the offset to {@code bytes} at which the sequence starts.
     * @param  length  the length of the byte sequence.
     * @return the {@code int} that results from the conversion of the byte sequence.
     * @since  1.0
     * @see    #bytesToIntBE(byte[], int, int)
     * @see    #intToBytesLE(int, byte[], int, int)
     */

    private static int bytesToIntLE(byte[] bytes, int offset, int length) {
        int i = offset + length;
        int value = bytes[--i];
        while (--i >= offset) {
            value <<= 8;
            value |= bytes[i] & 0xFF;
        }
        return value;
    }

    /**
     * Converts a big-endian sequence of bytes to an {@code int}.
     *
     * @param  bytes   the byte sequence that is to be converted.
     * @param  offset  the offset to {@code bytes} at which the sequence starts.
     * @param  length  the length of the byte sequence.
     * @return the {@code int} that results from the conversion of the byte sequence.
     * @since  1.0
     * @see    #bytesToIntLE(byte[], int, int)
     * @see    #intToBytesBE(int, byte[], int, int)
     */

    private static int bytesToIntBE(byte[] bytes, int offset, int length) {
        int endOffset = offset + length;
        int value = bytes[offset];
        while (++offset < endOffset) {
            value <<= 8;
            value |= bytes[offset] & 0xFF;
        }
        return value;
    }
}

Related

  1. BytesToDouble_With_Little_Endian(byte[] bytes)
  2. bytesToInt32(byte[] buffer, int byteOffset, boolean bigEndian)
  3. bytesToIntBigEndian(byte[] buf)
  4. bytesToIntLE(byte[] bytes, int offset, int length)
  5. bytesToLongLittleEndian(final byte[] vals, final int from)