Java Long Number Create ToLong(byte[] bytes, int offset)

Here you can find the source of ToLong(byte[] bytes, int offset)

Description

To Long

License

Apache License

Declaration

public static long ToLong(byte[] bytes, int offset) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static long ToLong(byte[] bytes, int offset) {
        return ((((long) bytes[offset] & 0xff) << 56)
                | (((long) bytes[offset + 1] & 0xff) << 48)
                | (((long) bytes[offset + 2] & 0xff) << 40)
                | (((long) bytes[offset + 3] & 0xff) << 32)
                | (((long) bytes[offset + 4] & 0xff) << 24)
                | (((long) bytes[offset + 5] & 0xff) << 16)
                | (((long) bytes[offset + 6] & 0xff) << 8) | (((long) bytes[offset + 7] & 0xff) << 0));
    }/*from   ww  w.j  a  v a2 s  .  co  m*/

    public static long ToLong(byte[] bytes) {
        return ((((long) bytes[0] & 0xff) << 56)
                | (((long) bytes[1] & 0xff) << 48)
                | (((long) bytes[2] & 0xff) << 40)
                | (((long) bytes[3] & 0xff) << 32)
                | (((long) bytes[4] & 0xff) << 24)
                | (((long) bytes[5] & 0xff) << 16)
                | (((long) bytes[6] & 0xff) << 8) | (((long) bytes[7] & 0xff) << 0));
    }

    public static long ToLong(int n1, int n2) {
        byte[] bytes = new byte[8];
        bytes[3] = (byte) (n1 & 0xff);
        bytes[2] = (byte) (n1 >> 8 & 0xff);
        bytes[1] = (byte) (n1 >> 16 & 0xff);
        bytes[0] = (byte) (n1 >> 24 & 0xff);

        bytes[7] = (byte) (n2 & 0xff);
        bytes[6] = (byte) (n2 >> 8 & 0xff);
        bytes[5] = (byte) (n2 >> 16 & 0xff);
        bytes[4] = (byte) (n2 >> 24 & 0xff);
        return ToLong(bytes);
    }
}

Related

  1. toLong(byte[] bytes)
  2. toLong(byte[] bytes, boolean bigEndian)
  3. toLong(byte[] bytes, int index)
  4. toLong(byte[] bytes, int index)
  5. toLong(byte[] bytes, int offset)
  6. toLong(byte[] bytes, int offset)
  7. toLong(byte[] bytes, int offset)
  8. toLong(byte[] data)
  9. toLong(byte[] data)