Java ByteBuffer to Int Array getIntBE(final ByteBuffer b, final int start, final int end)

Here you can find the source of getIntBE(final ByteBuffer b, final int start, final int end)

Description

Computes a number whereby the 1st byte is the most significant and the last byte is the least significant.

License

Open Source License

Parameter

Parameter Description
b The ByteBuffer
start The starting offset in b. The less significant byte
end The end index (included) in b

Return

an int number represented by the byte sequence.

Declaration

public static int getIntBE(final ByteBuffer b, final int start, final int end) 

Method Source Code

//package com.java2s;
/*/*  w ww  .java2 s  . c  om*/
 * Copyright (c) 2017 Eric A. Snell
 *
 * This file is part of eAlvaTag.
 *
 * eAlvaTag is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser
 * General Public License as published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * eAlvaTag is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with eAlvaTag.  If not,
 * see <http://www.gnu.org/licenses/>.
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Computes a number whereby the 1st byte is the most significant and the last
     * byte is the least significant.
     *
     * @param b     The ByteBuffer
     * @param start The starting offset in b. The less significant byte
     * @param end   The end index (included) in b
     *
     * @return an int number represented by the byte sequence.
     */
    public static int getIntBE(final ByteBuffer b, final int start, final int end) {
        return (int) getLongBE(b, start, end);
    }

    /**
     * Computes a number whereby the 1st byte is the most significant and the last
     * byte is the least significant.
     * <p>
     * So if storing a number which only requires one byte it will be stored in the last
     * byte.
     * <p>
     * Will fail if end - start >= 8, due to the limitations of the long type.
     */
    public static long getLongBE(final ByteBuffer b, final int start, final int end) {
        long number = 0;
        for (int i = 0; i < (end - start + 1); i++) {
            number += ((long) ((b.get(end - i) & 0xFF)) << i * 8);
        }

        return number;
    }
}

Related

  1. getInt(ByteBuffer intCalculator, byte[] bytes)
  2. getInt8(ByteBuffer input)
  3. getIntArray(ByteBuffer buffer, int size)
  4. getIntArray(ByteBuffer data, int offset, int length)
  5. getIntBE(ByteBuffer b, int start, int end)
  6. getIntByteBuffer(int id)
  7. getIntegerFromBuffer(ByteBuffer buffer, int offset, int size)
  8. getIntFromBack(ByteBuffer bb)
  9. getIntFromByteBuffer(ByteBuffer data)