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

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

Description

Converts byte representation to long.

License

Open Source License

Parameter

Parameter Description
b a parameter

Declaration

public static long byte2long(byte[] b) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  ww w.ja va2 s  .c  om*/
     * Converts byte representation to long.
     * Maximal byte size is 4.
     * 
     * 1st byte in b[0] is mapped to LSB in long.
     * 
     * @param b
     * @return 
     */
    public static long byte2long(byte[] b) {
        return ((long) b[0] & 0xff) | (((long) b[1] & 0xff) << 8) | (((long) b[2] & 0xff) << 16)
                | (((long) b[3] & 0xff) << 24);
    }

    /**
     * Converts byte representation to long form on given position - by idx.
     * Can be used multiple times on same long and OR results together.
     * 
     * LSB in long will be 1st bit in byte[0].
     * @param a
     * @param idx
     * @return 
     */
    public static long byte2long(byte a, int idx) {
        return (long) ((long) (a & 0xff) << (8 * idx));
    }
}

Related

  1. arr2long(byte[] b)
  2. atol(byte[] s)
  3. atol(byte[] s, int offset)
  4. bufferToLong(byte[] ioBuffer)
  5. byte2long(byte[] b)
  6. byte2Long(byte[] bytes, int offset)
  7. byte2Long(byte[] bytes, int offset)
  8. byte2long(byte[] data)