Java Long Number Create toLong(int numBytes, long value)

Here you can find the source of toLong(int numBytes, long value)

Description

Big endian.

License

Open Source License

Parameter

Parameter Description
numBytes 1-8
value non-negative

Return

an array of length numBytes

Declaration

public static byte[] toLong(int numBytes, long value) throws IllegalArgumentException 

Method Source Code

//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;
        }
    }
}

Related

  1. toLong(final String bitString)
  2. toLong(final String str, final long defaultValue)
  3. toLong(final String value)
  4. toLong(final String value)
  5. toLong(final String value)
  6. toLong(int value)
  7. toLong(int x, int z)
  8. toLong(int[] a)
  9. toLong(int[] x, int length, int M)