Java Long to Byte Array longToBytes(long v)

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

Description

Javascript doesn't support long data type, so it has to be converted to double before converting it to byte[].

License

Open Source License

Declaration

public static byte[] longToBytes(long v) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Oak Ridge National Laboratory.
 * 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
 ******************************************************************************/

public class Main {
    /**/*from   w  w w .  java 2 s.  c  o  m*/
     * Javascript doesn't support long data type, so it has to be converted to double before
     * converting it to byte[].
     */
    public static byte[] longToBytes(long v) {
        return doubleToBytes((double) v);
    }

    public static byte[] doubleToBytes(double d) {
        long l = Double.doubleToRawLongBits(d);
        byte[] r = new byte[8];
        for (int i = 0; i < 8; i++) {
            r[i] = (byte) ((l >>> (i * 8)) & 0xFF);
        }
        return r;
    }
}

Related

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