Java Byte Array Create toBytes(long value)

Here you can find the source of toBytes(long value)

Description

Convert long to 4 bytes array.

License

Apache License

Parameter

Parameter Description
value Value.

Return

Result

Declaration

public static byte[] toBytes(long value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//  w  ww .  j a v  a 2  s  .  c  o  m
     * Convert long to 4 bytes array. Same as byteValue().<br>
     * Example:<br>
     * value:258, result: {0x00, 0x00, 0x01, 0x02}.<br>
     * @param value Value.
     * @return Result
     */
    public static byte[] toBytes(long value) {
        byte[] result = new byte[8];
        result[0] = (byte) (value >> 56);
        result[1] = (byte) (value >> 48);
        result[2] = (byte) (value >> 40);
        result[3] = (byte) (value >> 32);
        result[4] = (byte) (value >> 24);
        result[5] = (byte) (value >> 16);
        result[6] = (byte) (value >> 8);
        result[7] = (byte) value;
        return result;
    }
}

Related

  1. toBytes(long size)
  2. toBytes(long v, byte[] writeBuffer, int o)
  3. toBytes(long val)
  4. toBytes(long value)
  5. toBytes(long value)
  6. toBytes(long value, int numBytes)
  7. toBytes(long... values)
  8. toBytes(Object objValue)
  9. toBytes(Object value)