Here you can find the source of toBytes(long n)
public static byte[] toBytes(long n)
//package com.java2s; /******************************************************************************* * Copyright (c) 2004, 2007 Boeing.//from w w w .j a va 2 s . c o m * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Boeing - initial API and implementation *******************************************************************************/ public class Main { public static byte[] toBytes(long n) { byte[] bytes = new byte[8]; toBytes(bytes, 0, n); return bytes; } public static void toBytes(byte[] bytes, int startPos, long n) { for (int i = startPos + 7; i >= startPos; i--) { bytes[i] = (byte) n; n >>>= 8; } } public static void toBytes(byte[] bytes, int startPos, int n) { for (int i = startPos + 3; i >= startPos; i--) { bytes[i] = (byte) n; n >>>= 8; } } }