Java Long to Byte Array longToByteArray(long value)

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

Description

Converts the given long value into a byte array.

License

Mozilla Public License

Parameter

Parameter Description
value Long value to convert to byte array.

Return

Byte array of the given long value (8 bytes length).

Declaration

public static byte[] longToByteArray(long value) 

Method Source Code

//package com.java2s;
/**//from  w ww .  ja  v a 2 s .  com
 * Copyright (c) 2014-2016 Digi International Inc.,
 * All rights not expressly granted are reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 * =======================================================================
 */

public class Main {
    /**
     * Converts the given long value into a byte array.
     * 
     * @param value Long value to convert to byte array.
     * 
     * @return Byte array of the given long value (8 bytes length).
     * 
     * @see #byteArrayToLong(byte[])
     */
    public static byte[] longToByteArray(long value) {
        return new byte[] { (byte) ((value >>> 56) & 0xFF), (byte) ((value >>> 48) & 0xFF),
                (byte) ((value >>> 40) & 0xFF), (byte) ((value >>> 32) & 0xFF), (byte) ((value >>> 24) & 0xFF),
                (byte) ((value >>> 16) & 0xFF), (byte) ((value >>> 8) & 0xFF), (byte) (value & 0xFF) };
    }
}

Related

  1. longToByteArray(long number)
  2. longToByteArray(long pSrc, byte[] pDst, int pOffset)
  3. longToByteArray(long source)
  4. longToByteArray(long v)
  5. longToByteArray(long value)
  6. longToByteArray(long value)
  7. longToByteArray(long value, byte[] buffer, int offset)
  8. longToByteArray(long value, byte[] byteArray)
  9. longToByteArray(long value, byte[] data, int offset)