Java Double to Byte Array putDouble(byte[] data, int i, boolean le, double v)

Here you can find the source of putDouble(byte[] data, int i, boolean le, double v)

Description

Puts an IEEE double-precision floating point number into an array of bytes.

License

Mozilla Public License

Parameter

Parameter Description
data The array of bytes to insert into.
i The index of the first byte of the double.
le True if the value is little-endian, false if the value is big-endian.
v The double at index <code>i</code>.

Declaration

public static void putDouble(byte[] data, int i, boolean le, double v) 

Method Source Code

//package com.java2s;
/*//from www  .  j  av  a2 s .  c om
 * Copyright &copy; 2010-2011 Rebecca G. Bettencourt / Kreative Software
 * <p>
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * <a href="http://www.mozilla.org/MPL/">http://www.mozilla.org/MPL/</a>
 * <p>
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * <p>
 * Alternatively, the contents of this file may be used under the terms
 * of the GNU Lesser General Public License (the "LGPL License"), in which
 * case the provisions of LGPL License are applicable instead of those
 * above. If you wish to allow use of your version of this file only
 * under the terms of the LGPL License and not to allow others to use
 * your version of this file under the MPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and
 * other provisions required by the LGPL License. If you do not delete
 * the provisions above, a recipient may use your version of this file
 * under either the MPL or the LGPL License.
 * @since KSFL 1.0
 * @author Rebecca G. Bettencourt, Kreative Software
 */

public class Main {
    /**
     * Puts an IEEE double-precision floating point number into an array of bytes.
     * @param data The array of bytes to insert into.
     * @param i The index of the first byte of the double.
     * @param v The double at index <code>i</code>.
     */
    public static void putDouble(byte[] data, int i, double v) {
        putLong(data, i, Double.doubleToRawLongBits(v));
    }

    /**
     * Puts an IEEE double-precision floating point number into an array of bytes.
     * @param data The array of bytes to insert into.
     * @param i The index of the first byte of the double.
     * @param le True if the value is little-endian, false if the value is big-endian.
     * @param v The double at index <code>i</code>.
     */
    public static void putDouble(byte[] data, int i, boolean le, double v) {
        if (le)
            putDoubleLE(data, i, v);
        else
            putDouble(data, i, v);
    }

    /**
     * Puts a 64-bit big-endian integer into an array of bytes.
     * @param data The array of bytes to insert into.
     * @param i The index of the first byte of the integer.
     * @param v The 64-bit big-endian integer at index <code>i</code>.
     */
    public static void putLong(byte[] data, int i, long v) {
        data[i + 0] = (byte) ((v >>> 56l) & 0xFFl);
        data[i + 1] = (byte) ((v >>> 48l) & 0xFFl);
        data[i + 2] = (byte) ((v >>> 40l) & 0xFFl);
        data[i + 3] = (byte) ((v >>> 32l) & 0xFFl);
        data[i + 4] = (byte) ((v >>> 24l) & 0xFFl);
        data[i + 5] = (byte) ((v >>> 16l) & 0xFFl);
        data[i + 6] = (byte) ((v >>> 8l) & 0xFFl);
        data[i + 7] = (byte) ((v >>> 0l) & 0xFFl);
    }

    /**
     * Puts a 64-bit big-endian integer into an array of bytes.
     * @param data The array of bytes to insert into.
     * @param i The index of the first byte of the integer.
     * @param le True if the value is little-endian, false if the value is big-endian.
     * @param v The 64-bit big-endian integer at index <code>i</code>.
     */
    public static void putLong(byte[] data, int i, boolean le, long v) {
        if (le)
            putLongLE(data, i, v);
        else
            putLong(data, i, v);
    }

    /**
     * Puts an IEEE double-precision floating point number into an array of bytes.
     * @param data The array of bytes to insert into.
     * @param i The index of the first byte of the double.
     * @param v The double at index <code>i</code>.
     */
    public static void putDoubleLE(byte[] data, int i, double v) {
        putLongLE(data, i, Double.doubleToRawLongBits(v));
    }

    /**
     * Puts a 64-bit little-endian integer into an array of bytes.
     * @param data The array of bytes to insert into.
     * @param i The index of the first byte of the integer.
     * @param v The 64-bit little-endian integer at index <code>i</code>.
     */
    public static void putLongLE(byte[] data, int i, long v) {
        data[i + 0] = (byte) ((v >>> 0l) & 0xFFl);
        data[i + 1] = (byte) ((v >>> 8l) & 0xFFl);
        data[i + 2] = (byte) ((v >>> 16l) & 0xFFl);
        data[i + 3] = (byte) ((v >>> 24l) & 0xFFl);
        data[i + 4] = (byte) ((v >>> 32l) & 0xFFl);
        data[i + 5] = (byte) ((v >>> 40l) & 0xFFl);
        data[i + 6] = (byte) ((v >>> 48l) & 0xFFl);
        data[i + 7] = (byte) ((v >>> 56l) & 0xFFl);
    }
}

Related

  1. doubleToBytes(double value)
  2. doubleToBytes(final double d)
  3. doubleToBytes(final double val)
  4. DoubleToBytes_With_Little_Endian(double number)
  5. putDouble(byte[] b, double val)
  6. putDouble(double x)