Java Long to Byte long2byte(byte[] b, long a)

Here you can find the source of long2byte(byte[] b, long a)

Description

Converts long representation to bytes.

License

Open Source License

Parameter

Parameter Description
b a parameter
a a parameter

Declaration

public static void long2byte(byte[] b, long a) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*w  ww. j a  v  a  2 s. co  m*/
     * Converts long representation to bytes.
     * Uses provided byte[] with length at least 4.
     * 
     * LSB in long will be 1st bit in byte[0].
     * @param b
     * @param a
     */
    public static void long2byte(byte[] b, long a) {
        b[0] = (byte) (a & 0xFF);
        b[1] = (byte) ((a >>> 8) & 0xFF);
        b[2] = (byte) ((a >>> 16) & 0xFF);
        b[3] = (byte) ((a >>> 24) & 0xFF);
    }

    /**
     * Converts long representation to bytes.
     * 
     * LSB in long will be 1st bit in byte[0].
     */
    public static byte[] long2byte(long a) {
        byte[] b = new byte[4];
        long2byte(b, a);
        return b;
    }

    /**
     * Converts long representation to bytes, returns selected part of long.
     * 
     * LSB in long will be 1st bit in byte[0].
     * @param a
     * @param idx
     * @return 
     */
    public static byte long2byte(long a, int idx) {
        return (byte) ((a >>> (8 * idx)) & 0xFF);
    }
}

Related

  1. long2Byte(byte[] bytes, long value, int offset)
  2. Long2Byte(long i)
  3. long2byte(long ival, byte b[], int offset)
  4. long2byte(long l)