Java Byte Array to Long bytesToLong(byte[] buf, int offset, int length, boolean asc)

Here you can find the source of bytesToLong(byte[] buf, int offset, int length, boolean asc)

Description

bytes To Long

License

Open Source License

Declaration

public static long bytesToLong(byte[] buf, int offset, int length, boolean asc) 

Method Source Code

//package com.java2s;
/**/* w w  w  .j a v a 2 s.co m*/
 * @(#)ByteUtils.java, 2013-2-24.
 * 
 * Copyright 2013 Netease, Inc. All rights reserved.
 * NETEASE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

public class Main {

    public static long bytesToLong(byte[] buf, int offset, int length, boolean asc) {
        if (buf == null) {
            throw new IllegalArgumentException("byte array is null!");
        }
        if (length > 8) {
            throw new IllegalArgumentException("byte array size > 8 !");
        }

        long l = 0;
        if (asc) {
            for (int i = offset; i < offset + length; i++) {
                l <<= 8;
                l |= ((long) buf[i] & 0xFF);
            }
        } else {
            for (int i = offset + length - 1; i >= 0; i--) {
                l <<= 8;
                l |= ((long) buf[i] & 0xFF);
            }
        }

        return l;
    }

    public static long bytesToLong(byte[] buf, boolean asc) {
        if (buf == null) {
            throw new IllegalArgumentException("byte array is null!");
        }

        return bytesToLong(buf, 0, buf.length, asc);
    }
}

Related

  1. bytesToLong(byte[] array, int offset)
  2. bytesToLong(byte[] b)
  3. bytesToLong(byte[] b)
  4. bytesToLong(byte[] b)
  5. bytesToLong(byte[] bs)
  6. bytesToLong(byte[] bytes)
  7. bytesToLong(byte[] bytes)
  8. bytesToLong(byte[] bytes)
  9. bytesToLong(byte[] bytes)