Java Convert via ByteBuffer longToBytes(long l, int size)

Here you can find the source of longToBytes(long l, int size)

Description

long To Bytes

License

Open Source License

Declaration

public static byte[] longToBytes(long l, int size) 

Method Source Code


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

import java.nio.ByteBuffer;

public class Main {
    public static byte[] longToBytes(long l, int size) {
        ByteBuffer buf = ByteBuffer.allocate(size);
        for (int i = 0; i < size; i++)
            buf.put(getNthByteFromLong(l, size - i - 1));
        return buf.array();
    }//from w  ww  .j  a  v a2  s.co m

    private static byte getNthByteFromLong(long l, int n) {
        return (byte) ((l & (0xff << n * 8)) >> (n * 8));
    }
}

Related

  1. longToByteArray(long value)
  2. longToByteArray(long[] array)
  3. longToBytes(final long value)
  4. longToBytes(long aLong)
  5. longToBytes(long l)
  6. longToBytes(long x)
  7. longToBytes(long x, byte[] dest, int offset)
  8. longToBytesNoLeadZeroes(long val)
  9. longToFourBytesFlip(long l)