Java Byte Array to Long byteToLong(byte[] b)

Here you can find the source of byteToLong(byte[] b)

Description

byte To Long

License

Apache License

Declaration

public static long byteToLong(byte[] b) 

Method Source Code

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

public class Main {
    public static long byteToLong(byte[] b) {
        if (b.length == 1) {
            return b[0] & 0xFF;
        } else if (b.length == 2) {
            return (long) ((b[0] & 0xFF) << 8) | (long) (b[1] & 0xFF);
        } else if (b.length == 3) {
            return (long) ((b[0] & 0xFF) << 16) | (long) ((b[1] & 0xFF) << 8) | (long) (b[2] & 0xFF);
        } else if (b.length == 4) {
            return (long) ((b[0] & 0xFF) << 24) | (long) ((b[1] & 0xFF) << 16) | (long) ((b[2] & 0xFF) << 8)
                    | (long) (b[3] & 0xFF);
        } else if (b.length == 5) {
            return (((long) b[0] & 0xFF) << 32) | (((long) b[1] & 0xFF) << 24) | (((long) b[2] & 0xFF) << 16)
                    | (((long) b[3] & 0xFF) << 8) | ((long) b[4] & 0xFF);
        } else if (b.length == 6) {
            return (((long) b[0] & 0xFF) << 40) | (((long) b[1] & 0xFF) << 32) | (((long) b[2] & 0xFF) << 24)
                    | (((long) b[3] & 0xFF) << 16) | (((long) b[4] & 0xFF) << 8) | ((long) b[5] & 0xFF);
        } else if (b.length == 7) {
            return (((long) b[0] & 0xFF) << 48) | (((long) b[1] & 0xFF) << 40) | (((long) b[2] & 0xFF) << 32)
                    | (((long) b[3] & 0xFF) << 24) | (((long) b[4] & 0xFF) << 16) | (((long) b[5] & 0xFF) << 8)
                    | ((long) b[6] & 0xFF);
        }/* ww w . j a va 2s. co m*/
        return (((long) b[0] & 0xFF) << 56) | (((long) b[1] & 0xFF) << 48) | (((long) b[2] & 0xFF) << 40)
                | (((long) b[3] & 0xFF) << 32) | (((long) b[4] & 0xFF) << 24) | (((long) b[5] & 0xFF) << 16)
                | (((long) b[6] & 0xFF) << 8) | ((long) b[7] & 0xFF);
    }

    public static long byteToLong(byte[] b, int offset, int length) {
        if (length == 1) {
            return b[offset + 0] & 0xFF;
        } else if (length == 2) {
            return (long) ((b[offset + 0] & 0xFF) << 8) | (long) (b[offset + 1] & 0xFF);
        } else if (length == 3) {
            return (long) ((b[offset + 0] & 0xFF) << 16) | (long) ((b[offset + 1] & 0xFF) << 8)
                    | (long) (b[offset + 2] & 0xFF);
        } else if (length == 4) {
            return (long) ((b[offset + 0] & 0xFF) << 24) | (long) ((b[offset + 1] & 0xFF) << 16)
                    | (long) ((b[offset + 2] & 0xFF) << 8) | (long) (b[offset + 3] & 0xFF);
        } else if (length == 5) {
            return (((long) b[offset + 0] & 0xFF) << 32) | (((long) b[offset + 1] & 0xFF) << 24)
                    | (((long) b[offset + 2] & 0xFF) << 16) | (((long) b[offset + 3] & 0xFF) << 8)
                    | ((long) b[offset + 4] & 0xFF);
        } else if (length == 6) {
            return (((long) b[offset + 0] & 0xFF) << 40) | (((long) b[offset + 1] & 0xFF) << 32)
                    | (((long) b[offset + 2] & 0xFF) << 24) | (((long) b[offset + 3] & 0xFF) << 16)
                    | (((long) b[offset + 4] & 0xFF) << 8) | ((long) b[offset + 5] & 0xFF);
        } else if (length == 7) {
            return (((long) b[offset + 0] & 0xFF) << 48) | (((long) b[offset + 1] & 0xFF) << 40)
                    | (((long) b[offset + 2] & 0xFF) << 32) | (((long) b[offset + 3] & 0xFF) << 24)
                    | (((long) b[offset + 4] & 0xFF) << 16) | (((long) b[offset + 5] & 0xFF) << 8)
                    | ((long) b[offset + 6] & 0xFF);
        }
        return (((long) b[offset + 0] & 0xFF) << 56) | (((long) b[offset + 1] & 0xFF) << 48)
                | (((long) b[offset + 2] & 0xFF) << 40) | (((long) b[offset + 3] & 0xFF) << 32)
                | (((long) b[offset + 4] & 0xFF) << 24) | (((long) b[offset + 5] & 0xFF) << 16)
                | (((long) b[offset + 6] & 0xFF) << 8) | ((long) b[offset + 7] & 0xFF);
    }
}

Related

  1. bytesToLong(final byte[] b)
  2. bytesToLong(final byte[] bytes)
  3. bytesToLong(final byte[] bytes)
  4. bytesToLong(final byte[] data, final int offset)
  5. bytesToLongLE(byte[] bytes, int off)
  6. byteToLong(byte[] b)
  7. byteToLong(byte[] buf, int off)
  8. byteToLong(byte[] byteArray)
  9. byteToLong(byte[] bytesToConvert)