Java Convert via ByteBuffer toByteArrayFromLong(long longValue)

Here you can find the source of toByteArrayFromLong(long longValue)

Description

Converts a long (which in Java is a signed eight bytes value) into a byte array

License

Open Source License

Parameter

Parameter Description
longValue The long value which is to be converted to a byte array

Return

The byte array corresponding to the given long value

Declaration

public static byte[] toByteArrayFromLong(long longValue) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright (c) 2016 Dr.-Ing. Marc M?ltin.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:/* ww w. ja  v a 2 s . c  om*/
 *    Dr.-Ing. Marc M?ltin - initial API and implementation and initial documentation
 *******************************************************************************/

import java.nio.ByteBuffer;

public class Main {
    /**
     * Converts a long (which in Java is a signed eight bytes value) into a byte array
     * @param longValue The long value which is to be converted to a byte array
     * @return The byte array corresponding to the given long value
     */
    public static byte[] toByteArrayFromLong(long longValue) {
        return ByteBuffer.allocate(8).putLong(longValue).array();
    }
}

Related

  1. toByteArray(String value)
  2. toByteArray(UUID uniqueId)
  3. toByteArray2(String filename)
  4. toByteArray3(String filename)
  5. toByteArrayFromInt(int intValue, boolean shortSize)
  6. toBytes(BigDecimal number, int byteLength)
  7. toBytes(char[] ch)
  8. toBytes(char[] chars)
  9. toBytes(char[] string)