Here you can find the source of toLong(int numBytes, long value)
Parameter | Description |
---|---|
numBytes | 1-8 |
value | non-negative |
public static byte[] toLong(int numBytes, long value) throws IllegalArgumentException
//package com.java2s; //License from project: Open Source License public class Main { /**//ww w . j a v a 2 s .co m * Big endian. * * @param numBytes 1-8 * @param value non-negative * @return an array of length numBytes */ public static byte[] toLong(int numBytes, long value) throws IllegalArgumentException { byte val[] = new byte[numBytes]; toLong(val, 0, numBytes, value); return val; } /** * Big endian. * * @param numBytes 1-8 * @param value non-negative */ public static void toLong(byte target[], int offset, int numBytes, long value) throws IllegalArgumentException { if (numBytes <= 0 || numBytes > 8) throw new IllegalArgumentException("Invalid number of bytes"); if (value < 0) throw new IllegalArgumentException("Negative value not allowed"); for (int i = offset + numBytes - 1; i >= offset; i--) { target[i] = (byte) value; value >>= 8; } } }