Java Long Number Array Create toLongArray(byte[] data)

Here you can find the source of toLongArray(byte[] data)

Description

to Long Array

License

Open Source License

Declaration

public static long[] toLongArray(byte[] data) 

Method Source Code

//package com.java2s;
/*//from  w ww.j  a  va 2s.  c om
 * Copyright (C) 2014 Jesse Caulfield 
 *
 * 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 {
    public static long[] toLongArray(byte[] data) {
        if ((data == null) || (data.length % 8 != 0)) {
            return null;
        }
        long[] lngs = new long[data.length / 8];
        for (int i = 0; i < lngs.length; i++) {
            lngs[i] = toLong(new byte[] { data[(i * 8)], data[(i * 8) + 1], data[(i * 8) + 2], data[(i * 8) + 3],
                    data[(i * 8) + 4], data[(i * 8) + 5], data[(i * 8) + 6], data[(i * 8) + 7], });
        }
        return lngs;
    }

    /**
     * Convert 8 bytes into a long.
     * <p>
     * This converts the 8 bytes into a long.
     *
     * @param byte7 The byte to be left-shifted 56 bits.
     * @param byte6 The byte to be left-shifted 48 bits.
     * @param byte5 The byte to be left-shifted 40 bits.
     * @param byte4 The byte to be left-shifted 32 bits.
     * @param byte3 The byte to be left-shifted 24 bits.
     * @param byte2 The byte to be left-shifted 16 bits.
     * @param byte1 The byte to be left-shifted 8 bits.
     * @param byte0 The byte that will not be left-shifted.
     * @return A long representing the bytes.
     */
    public static long toLong(byte byte7, byte byte6, byte byte5, byte byte4, byte byte3, byte byte2, byte byte1,
            byte byte0) {
        return toLong(toInt(byte7, byte6, byte5, byte4), toInt(byte3, byte2, byte1, byte0));
    }

    /**
     * Convert 4 shorts into a long.
     * <p>
     * This converts the 4 shorts into a long.
     *
     * @param short3 The short to be left-shifted 48 bits.
     * @param short2 The short to be left-shifted 32 bits.
     * @param short1 The short to be left-shifted 16 bits.
     * @param short0 The short that will not be left-shifted.
     * @return A long representing the shorts.
     */
    public static long toLong(short short3, short short2, short short1, short short0) {
        return toLong(toInt(short3, short2), toInt(short1, short0));
    }

    /**
     * Convert 2 ints into a long.
     * <p>
     * This converts the 2 ints into a long.
     *
     * @param msi The Most Significant Int.
     * @param lsi The Least Significant Int.
     * @return A long representing the ints.
     */
    public static long toLong(int msi, int lsi) {
        /*
         * We can't represent a mask for the MSI, but that's ok, we don't really
         * need one; left-shifting sets the low bits to 0.
         */
        return (long) msi << 32 | (long) 0x00000000ffffffff & (long) lsi;
    }

    public static long toLong(byte[] data) {
        if ((data == null) || (data.length != 8)) {
            return 0x0;
        }
        return ( /**
                 * (Below) convert to longs before shift because digits are lost
                 * with ints beyond the 32-bit limit
                 */
        (long) (0xff & data[0]) << 56 | (long) (0xff & data[1]) << 48 | (long) (0xff & data[2]) << 40
                | (long) (0xff & data[3]) << 32 | (long) (0xff & data[4]) << 24 | (long) (0xff & data[5]) << 16
                | (long) (0xff & data[6]) << 8 | (long) (0xff & data[7]));
    }

    /**
     * Convert 4 bytes into an int.
     * <p>
     * This converts the 4 bytes into an int.
     *
     * @param byte3 The byte to be left-shifted 24 bits.
     * @param byte2 The byte to be left-shifted 16 bits.
     * @param byte1 The byte to be left-shifted 8 bits.
     * @param byte0 The byte that will not be left-shifted.
     * @return An int representing the bytes.
     */
    public static int toInt(byte byte3, byte byte2, byte byte1, byte byte0) {
        return toInt(toShort(byte3, byte2), toShort(byte1, byte0));
    }

    /**
     * Convert 2 shorts into an int.
     * <p>
     * This converts the 2 shorts into an int.
     *
     * @param mss The Most Significant Short.
     * @param lss The Least Significant Short.
     * @return An int representing the shorts.
     */
    public static int toInt(short mss, short lss) {
        return ((0xffff0000 & mss << 16) | (0x0000ffff & (int) lss));
    }

    public static int toInt(byte[] data) {
        if ((data == null) || (data.length != 4)) {
            return 0x0;
        }
        return ( // NOTE: type cast not necessary for int
        (0xff & data[0]) << 24 | (0xff & data[1]) << 16 | (0xff & data[2]) << 8 | (0xff & data[3]));
    }

    /**
     * Convert 2 bytes into a short.
     * <p>
     * This converts the 2 bytes into a short. The msb will be the high byte (8
     * bits) of the short, and the lsb will be the low byte (8 bits) of the short.
     *
     * @param msb The Most Significant Byte.
     * @param lsb The Least Significant Byte.
     * @return A short representing the bytes.
     */
    public static short toShort(byte msb, byte lsb) {
        return (short) ((0xff00 & (short) (msb << 8)) | (0x00ff & (short) lsb));
    }

    public static short toShort(byte[] data) {
        if ((data == null) || (data.length != 2)) {
            return 0x0;
        }
        return (short) ((0xff & data[0]) << 8 | (0xff & data[1]));
    }
}

Related

  1. toLongA(byte[] data)
  2. toLongArray(boolean[] array)
  3. toLongArray(byte[] array)
  4. toLongArray(byte[] byteArray)
  5. toLongArray(byte[] byteArray)
  6. toLongArray(double[][] array)
  7. toLongArray(final byte[] array)
  8. toLongArray(final int[] in)
  9. toLongArray(final long[] longs)