Java Long to Byte Array longToBytes(long number)

Here you can find the source of longToBytes(long number)

Description

Converts a long number to a byte array

License

Open Source License

Parameter

Parameter Description
number a parameter

Return

bytes as long value

Declaration

public static byte[] longToBytes(long number) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  w w w  .  j  a v  a  2 s . co  m
     * Converts a long number to a byte array
     * @param number
     * @return bytes as long value
     */
    public static byte[] longToBytes(long number) {
        byte[] ret = new byte[8];
        ret[0] = (byte) ((number >>> 8 * 7) & 0xFF);
        ret[1] = (byte) ((number >> 8 * 6) & 0xFF);
        ret[2] = (byte) ((number >> 8 * 5) & 0xFF);
        ret[3] = (byte) ((number >> 8 * 4) & 0xFF);
        ret[4] = (byte) ((number >>> 8 * 3) & 0xFF);
        ret[5] = (byte) ((number >> 8 * 2) & 0xFF);
        ret[6] = (byte) ((number >> 8 * 1) & 0xFF);
        ret[7] = (byte) (number & 0xFF);
        return ret;
    }
}

Related

  1. longToBytes(long n)
  2. longToBytes(long n)
  3. longToBytes(long n, byte b[], int offset)
  4. longToBytes(long num)
  5. longToBytes(long num, byte[] data, int index)
  6. longToBytes(long v)
  7. longToBytes(long v)
  8. longToBytes(long v, byte[] b)
  9. longToBytes(long v, byte[] bytes)