Takes some bytes and returns the Big Endian version of those bytes as an integer. - Java java.lang

Java examples for java.lang:String Endian

Description

Takes some bytes and returns the Big Endian version of those bytes as an integer.

Demo Code

/*// ww w  .j av a  2s  .c om
 *  Copyright 2006 The National Library of New Zealand
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
//package com.java2s;

public class Main {
    /**
     * Takes some bytes and returns the Big Endian version of those bytes as an
     * integer.
     * 
     * @param data
     *            the block of data containing the number
     * @param offset
     *            the offset to start processing from
     * @param size
     *            the sizer of the number in bytes
     * @return the integer.
     */
    protected static long getBigEndian(final byte[] data, final int offset,
            final int size) {
        long result = 0;

        for (int j = offset; j < offset + size; j++) {
            result <<= 8;
            result |= 0xff & data[j];
        }
        return result;
    }
}

Related Tutorials