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

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

Description

byte To Long

License

Open Source License

Declaration

public static long byteToLong(byte[] i_Value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {

    public static long byteToLong(byte[] i_Value) {
        if (i_Value.length >= 8) {
            return (((long) (byteToInt(substr(i_Value, 0, 4)))) << 32)
                    + ((byteToInt(substr(i_Value, 4, 4)) & 0xFFFFFFFFL));
        } else if (i_Value.length <= 4) {
            return (byteToInt(substr(i_Value, 4, 4)) & 0xFFFFFFFFL);
        } else if (i_Value.length > 4) {
            return (((long) (byteToInt(substr(i_Value, 0, i_Value.length - 4)))) << 32)
                    + ((byteToInt(substr(i_Value, i_Value.length - 4, 4)) & 0xFFFFFFFFL));
        } else {//from  w  w  w .ja v  a 2  s .  c o m
            return 0;
        }
    }

    public static int byteToInt(byte[] i_Value) {
        if (i_Value.length >= 4) {
            return (((i_Value[0] & 0xFF) << 24) + ((i_Value[1] & 0xFF) << 16) + ((i_Value[2] & 0xFF) << 8)
                    + ((i_Value[3] & 0xFF) << 0));
        } else if (i_Value.length == 3) {
            return (((i_Value[0] & 0xFF) << 16) + ((i_Value[1] & 0xFF) << 8) + ((i_Value[2] & 0xFF) << 0));
        } else if (i_Value.length == 2) {
            return (((i_Value[0] & 0xFF) << 8) + ((i_Value[1] & 0xFF) << 0));
        } else if (i_Value.length == 1) {
            return (((i_Value[0] & 0xFF) << 0));
        } else {
            return 0;
        }
    }

    public static byte[] substr(byte[] i_Value, int i_BeginIndex) {
        return substr(i_Value, i_BeginIndex, i_Value.length - i_BeginIndex);
    }

    public static byte[] substr(byte[] i_Value, int i_BeginIndex, int i_SubLen) {
        int v_BeginIndex = i_BeginIndex % i_Value.length;
        int v_EndIndex = v_BeginIndex + i_SubLen;
        int v_NewArrLen = v_EndIndex <= i_Value.length ? i_SubLen : i_Value.length - v_BeginIndex - 1;
        v_EndIndex = v_BeginIndex + v_NewArrLen;
        byte[] v_Ret = new byte[v_NewArrLen];

        for (int v_Index = v_BeginIndex; v_Index < v_EndIndex; v_Index++) {
            v_Ret[v_Index - v_BeginIndex] = i_Value[v_Index];
        }

        return v_Ret;
    }
}

Related

  1. byteToLong(byte[] b)
  2. byteToLong(byte[] b)
  3. byteToLong(byte[] buf, int off)
  4. byteToLong(byte[] byteArray)
  5. byteToLong(byte[] bytesToConvert)
  6. byteToLong(byte[] values)
  7. byteToLong(final byte[] b)