Java Long to Byte Array longToByteArray(long value)

Here you can find the source of longToByteArray(long value)

Description

long To Byte Array

License

LGPL

Declaration

private static byte[] longToByteArray(long value) 

Method Source Code

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

public class Main {
    private static byte[] longToByteArray(long value) {
        byte[] result = new byte[8];

        // FYI - The effect of this operation is to break
        // down the long into an array of eight bytes.
        ////from  ww w  . j  av a 2s . com
        // What's going on: The FF selects selects the byte
        // of interest within value. The the >> shifts the
        // target bits to the right the desired result. The
        // shift ensures the result will fit into a single
        // 8-bit byte. Depending upon the byte of interest
        // it must be shifted appropriately so it's always
        // in the lower-order 8-bits.

        result[0] = (byte) (value & 0x00000000000000FFL);
        result[1] = (byte) ((value & 0x000000000000FF00L) >> 8);
        result[2] = (byte) ((value & 0x0000000000FF0000L) >> 16);
        result[3] = (byte) ((value & 0x00000000FF000000L) >> 24);
        result[4] = (byte) ((value & 0x000000FF00000000L) >> 32);
        result[5] = (byte) ((value & 0x0000FF0000000000L) >> 40);
        result[6] = (byte) ((value & 0x00FF000000000000L) >> 48);
        result[7] = (byte) ((value & 0xFF00000000000000L) >> 56);

        return result;
    }
}

Related

  1. longToByteArray(long pSrc, byte[] pDst, int pOffset)
  2. longToByteArray(long source)
  3. longToByteArray(long v)
  4. longToByteArray(long value)
  5. longToByteArray(long value)
  6. longToByteArray(long value, byte[] buffer, int offset)
  7. longToByteArray(long value, byte[] byteArray)
  8. longToByteArray(long value, byte[] data, int offset)
  9. longToByteArray(long value, byte[] dest)