Java Double to Byte Array doubleToBytes(double d, byte[] data, int[] offset)

Here you can find the source of doubleToBytes(double d, byte[] data, int[] offset)

Description

Write the bytes representing d into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.

License

Open Source License

Parameter

Parameter Description
d the <code>double</code> to encode
data The byte array to store into, or <code>null</code>.
offset A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.

Declaration

public static final void doubleToBytes(double d, byte[] data, int[] offset) 

Method Source Code

//package com.java2s;
/*//from  ww  w  .  ja  v  a2s .  c om
 * JGrass - Free Open Source Java GIS http://www.jgrass.org 
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

public class Main {
    public static final int SIZE_LONG = 8;

    /**
     * Write the bytes representing <code>d</code> into the byte array
     * <code>data</code>, starting at index <code>offset [0]</code>, and
     * increment <code>offset [0]</code> by the number of bytes written; if
     * <code>data == null</code>, increment <code>offset [0]</code> by the
     * number of bytes that would have been written otherwise.
     *
     * @param d the <code>double</code> to encode
     * @param data The byte array to store into, or <code>null</code>.
     * @param offset A single element array whose first element is the index in
     *         data to begin writing at on function entry, and which on
     *         function exit has been incremented by the number of bytes
     *         written.
     */
    public static final void doubleToBytes(double d, byte[] data, int[] offset) {
        long bits = Double.doubleToLongBits(d);
        longToBytes(bits, data, offset);
    }

    /**
     * Write the bytes representing <code>l</code> into the byte array
     * <code>data</code>, starting at index <code>offset [0]</code>, and
     * increment <code>offset [0]</code> by the number of bytes written; if
     * <code>data == null</code>, increment <code>offset [0]</code> by the
     * number of bytes that would have been written otherwise.
     *
     * @param l the <code>long</code> to encode
     * @param data The byte array to store into, or <code>null</code>.
     * @param offset A single element array whose first element is the index in
     *         data to begin writing at on function entry, and which on
     *         function exit has been incremented by the number of bytes
     *         written.
     */
    public static final void longToBytes(long l, byte[] data, int[] offset) {
        /**
         * TODO: We use network-order within OceanStore, but temporarily
         * supporting intel-order to work with some JNI code until JNI code is
         * set to interoperate with network-order.
         */
        if (data != null) {
            for (int j = (offset[0] + SIZE_LONG) - 1; j >= offset[0]; --j) {
                data[j] = (byte) l;
                l >>= 8;
            }
        }

        offset[0] += SIZE_LONG;
    }
}

Related

  1. doubleToByteArrayBE(double data)
  2. doubleToBytes(double d)
  3. doubleToBytes(double d)
  4. doubleToBytes(double d)
  5. doubleToBytes(double d, byte[] bytes, int off, boolean bigEndian)
  6. doubleToBytes(double dnum, byte[] bytes, int startIndex)
  7. doubleToBytes(double v, byte[] bytes)
  8. doubleToBytes(double v, byte[] bytes, int off)
  9. doubleToBytes(double value)