Java Byte Array Convert To bytesToNumber(byte[] buffer, int start, int length)

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

Description

Reconstructs a number that is represented by more than one byte in a network packet in big-endian order.

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

public static long bytesToNumber(byte[] buffer, int start, int length) 

Method Source Code

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

public class Main {
    /**/*from  www. j  a va 2  s. co m*/
     * Reconstructs a number that is represented by more than one byte in a network packet in big-endian order.
     *
     * @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
     */
    public static long bytesToNumber(byte[] buffer, int start, int length) {
        long result = 0;
        for (int index = start; index < start + length; 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 abyte0[], int offset)
  2. bytesToIpString(byte[] bytes)
  3. bytesToLength(final byte bytes[])
  4. bytesToMac(byte[] bytes)
  5. bytesToMac(final byte[] bytes)
  6. bytesToPdu(byte[] bytes)
  7. bytesToSectors(long bytes)
  8. bytesToSignedInt(byte bb1, byte bb2)
  9. bytesToStore(int bits)