Java Byte Array to Endian bytesToNumberLittleEndian(byte[] buffer, int start, int length)

Here you can find the source of bytesToNumberLittleEndian(byte[] buffer, int start, int length)

Description

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.

License

Open Source License

Parameter

Parameter Description
buffer the byte array containing the packet data
start the index of the first byte containing a numeric value
length the number of bytes making up the value

Return

the reconstructed number

Declaration

@SuppressWarnings("SameParameterValue")
public static long bytesToNumberLittleEndian(byte[] buffer, int start, int length) 

Method Source Code

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

public class Main {
    /**/*  w w w.  j a  v a2s .  c  om*/
     * 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.
     *
     * @param buffer the byte array containing the packet data
     * @param start the index of the first byte containing a numeric value
     * @param length the number of bytes making up the value
     * @return the reconstructed number
     */
    @SuppressWarnings("SameParameterValue")
    public static long bytesToNumberLittleEndian(byte[] buffer, int start, int length) {
        long result = 0;
        for (int index = start + length - 1; index >= start; index--) {
            result = (result << 8) + unsign(buffer[index]);
        }
        return result;
    }

    /**
     * Converts a signed byte to its unsigned int equivalent in the range 0-255.
     *
     * @param b a byte value to be considered an unsigned integer
     *
     * @return the unsigned version of the byte
     */
    public static int unsign(byte b) {
        return b & 0xff;
    }
}

Related

  1. bytesToInt(byte[] bytes, int offset, int length, boolean littleEndian)
  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)
  6. bytesToTag(byte[] bytes, int off, boolean bigEndian)
  7. bytesToXBigEndian(byte[] in, int offset, int size)