Here you can find the source of bytesToInt(byte[] bytes, int offset, int length, boolean littleEndian)
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. |
protected static int bytesToInt(byte[] bytes, int offset, int length, boolean littleEndian)
//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; } }