Java Double Array to Byte Array doublesToBytes(double[] doubles)

Here you can find the source of doublesToBytes(double[] doubles)

Description

Converts an array of doubles into an array of bytes Byte order: little endian

License

Open Source License

Parameter

Parameter Description
doubles an array of doubles

Return

the corresponding array of bytes

Declaration

public static byte[] doublesToBytes(double[] doubles) 

Method Source Code

//package com.java2s;
/**/* w w w .j  a  v a 2s  .c o  m*/
 * OrbisGIS is a GIS application dedicated to scientific spatial simulation.
 * This cross-platform GIS is developed at French IRSTV institute and is able to
 * manipulate and create vector and raster spatial information.
 *
 * OrbisGIS is distributed under GPL 3 license. It is produced by the "Atelier SIG"
 * team of the IRSTV Institute <http://www.irstv.fr/> CNRS FR 2488.
 *
 * Copyright (C) 2007-2012 IRSTV (FR CNRS 2488)
 *
 * This file is part of OrbisGIS.
 *
 * OrbisGIS is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * OrbisGIS 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * OrbisGIS. If not, see <http://www.gnu.org/licenses/>.
 *
 * For more information, please consult: <http://www.orbisgis.org/>
 * or contact directly:
 * info_at_ orbisgis.org
 */

public class Main {
    /**
     * Converts an array of doubles into an array of bytes
     * 
     * Byte order: little endian
     * 
     * @param doubles an array of doubles
     * @return the corresponding array of bytes
     */
    public static byte[] doublesToBytes(double[] doubles) {
        long[] longs = new long[doubles.length];
        for (int i = 0; i < doubles.length; i++) {
            longs[i] = Double.doubleToLongBits(doubles[i]);
        }
        return longsToBytes(longs);
    }

    /**
     * Converts an array of longs into an array of bytes
     * 
     * Byte order: little endian
     * 
     * @param longs an array of longs
     * @return the corresponding array of bytes
     */
    public static byte[] longsToBytes(long[] longs) {
        byte[] ret = new byte[longs.length * 8];
        for (int i = 0; i < longs.length; i++) {
            ret[i * 8] = (byte) (longs[i]);
            ret[i * 8 + 1] = (byte) (longs[i] >>> 8);
            ret[i * 8 + 2] = (byte) (longs[i] >>> 16);
            ret[i * 8 + 3] = (byte) (longs[i] >>> 24);
            ret[i * 8 + 4] = (byte) (longs[i] >>> 32);
            ret[i * 8 + 5] = (byte) (longs[i] >>> 40);
            ret[i * 8 + 6] = (byte) (longs[i] >>> 48);
            ret[i * 8 + 7] = (byte) (longs[i] >>> 56);
        }

        return ret;
    }
}

Related

  1. doublesToBytes(double[] d)
  2. doublesToBytes(final double[] array)