Java Long to Byte Array longToByteArray(long v)

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

Description

Converts a long to a byte array of length 8

License

Open Source License

Parameter

Parameter Description
v long value to convert

Return

a byte array of length 8

Declaration

public static byte[] longToByteArray(long v) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2011-2013 Pushing Inertia
 * All rights reserved.  http://pushinginertia.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.//from w w w . j a v  a  2s  .  c o  m
 */

public class Main {
    /**
     * Converts a long to a byte array of length 8
     * @param v long value to convert
     * @return a byte array of length 8
     */
    public static byte[] longToByteArray(long v) {
        final byte[] writeBuffer = new byte[8];
        writeBuffer[0] = (byte) (v >>> 56);
        writeBuffer[1] = (byte) (v >>> 48);
        writeBuffer[2] = (byte) (v >>> 40);
        writeBuffer[3] = (byte) (v >>> 32);
        writeBuffer[4] = (byte) (v >>> 24);
        writeBuffer[5] = (byte) (v >>> 16);
        writeBuffer[6] = (byte) (v >>> 8);
        writeBuffer[7] = (byte) (v >>> 0);
        return writeBuffer;
    }
}

Related

  1. longToByteArray(long l)
  2. longToByteArray(long longHi, long longLo, byte[] array, int offset)
  3. longToByteArray(long number)
  4. longToByteArray(long pSrc, byte[] pDst, int pOffset)
  5. longToByteArray(long source)
  6. longToByteArray(long value)
  7. longToByteArray(long value)
  8. longToByteArray(long value)
  9. longToByteArray(long value, byte[] buffer, int offset)