Here you can find the source of longToBytes(long l, int size)
public static byte[] longToBytes(long l, int size)
//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)); } }