Java Convert via ByteBuffer ToLongArray(byte[] data)

Here you can find the source of ToLongArray(byte[] data)

Description

To Long Array

License

Open Source License

Declaration

private static long[] ToLongArray(byte[] data) 

Method Source Code

//package com.java2s;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    private static long[] ToLongArray(byte[] data) {
        int n = (data.length % 8 == 0 ? 0 : 1) + data.length / 8;
        long[] result = new long[n];

        for (int i = 0; i < n - 1; i++) {
            result[i] = bytes2long(data, i * 8);
        }//from  www .  j a  va 2 s.  c om

        byte[] buffer = new byte[8];
        for (int i = 0, j = (n - 1) * 8; j < data.length; i++, j++) {
            buffer[i] = data[j];
        }
        result[n - 1] = bytes2long(buffer, 0);

        return result;
    }

    private static long[] ToLongArray(String data) {
        int len = data.length() / 16;
        long[] result = new long[len];
        for (int i = 0; i < len; i++) {
            result[i] = new BigInteger(data.substring(i * 16, i * 16 + 16),
                    16).longValue();
        }
        return result;
    }

    public static long bytes2long(byte[] b, int index) {
        ByteBuffer buffer = ByteBuffer.allocate(8).order(
                ByteOrder.LITTLE_ENDIAN);
        buffer.put(b, index, 8);
        return buffer.getLong(0);
    }
}

Related

  1. toLMBCS(final String value)
  2. toLong(byte data[])
  3. toLong(byte[] array)
  4. toLong(byte[] bytes)
  5. toLongArray(byte[] b)
  6. toLongArray(byte[] ids)
  7. toMappedBuffer(File file)
  8. toMappedBuffer(File file)
  9. toMappedBuffer(File file, long start, long end)