Java Long Number Create toLong(byte[] vint)

Here you can find the source of toLong(byte[] vint)

Description

to Long

License

Open Source License

Declaration

public static final long toLong(byte[] vint) 

Method Source Code

//package com.java2s;
/*/*ww  w .  j a  v a  2  s . c o  m*/
 *  Copyright (C) 2010 Shashank Tulsyan
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program 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 General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    private static final byte sig[] = { 0x0, //null
            -128, //1000 0000
            0x40, //0100 0000
            0x20, //0010 0000
            0x10, //0001 0000
            0x8, //0000 1000
            0x4, //0000 0100
            0x2, //0000 0010
            0x1, //0000 0001 
    };

    public static final long toLong(byte[] vint) {
        if (vint.length == 0)
            throw new ArrayIndexOutOfBoundsException(
                    "size of the vint array is zero");

        byte firstByte = vint[0];
        if (firstByte == 0) {
            return 0;
        } //null stored

        int numOfBytes = getNumberOfBytesInVint(firstByte);

        //remove indicator bit from firstByte

        //data types in java are signed
        //a byte might get coverted into negative integer
        //that is why anding to 0xff is needed
        firstByte = (byte) ((firstByte & 0xFF) ^ (sig[numOfBytes] & 0xFF));
        long ret = 0;

        int i = 0;
        for (; i < numOfBytes - 1; i++) {
            ret |= (vint[numOfBytes - 1 - i] & 0xFF) << (8 * i);
        }
        ret |= (firstByte & 0xFF) << (8 * (numOfBytes - 1));
        return ret;
    }

    public static final int getNumberOfBytesInVint(byte firstByte) {
        if (firstByte == 0)
            return 1; //null stored

        int numOfDig = 1, t;
        for (int i = 1; i < sig.length; i++) {
            //data types in java are signed
            //a byte might get coverted into negative integer
            //that is why anding to 0xff is needed
            t = (firstByte & 0xFF) & (sig[i] & 0xFF);
            if (t != 0) {
                numOfDig = i;
                break;
            }
        }

        return numOfDig;
    }
}

Related

  1. toLong(byte[] src)
  2. toLong(byte[] src, int srcPos)
  3. toLong(byte[] v)
  4. toLong(byte[] value)
  5. toLong(byte[] value)
  6. toLong(double val)
  7. toLong(double[] in, long[] out)
  8. toLong(double[] v)
  9. toLong(final byte[] array, final int offset, final int length)