Java Float to Byte Array floatToBytes(float theFloat)

Here you can find the source of floatToBytes(float theFloat)

Description

Converts a float into an array of bytes Byte order: big endian

License

Open Source License

Parameter

Parameter Description
theFloat a float

Return

the corresponding array of bytes

Declaration

public static byte[] floatToBytes(float theFloat) 

Method Source Code

//package com.java2s;
/**/*w  ww.ja  v  a 2 s .  co  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 {
    private static final int BYTESIZE = 0xFF;

    /**
     * Converts a float into an array of bytes
     * 
     * Byte order: big endian
     * 
     * @param theFloat a float
     * @return the corresponding array of bytes
     */
    public static byte[] floatToBytes(float theFloat) {
        byte[] ret = new byte[4];

        int floatAsInt = Float.floatToIntBits(theFloat);
        ret[0] = (byte) ((floatAsInt >>> 24) & BYTESIZE);
        ret[1] = (byte) ((floatAsInt >>> 16) & BYTESIZE);
        ret[2] = (byte) ((floatAsInt >>> 8) & BYTESIZE);
        ret[3] = (byte) ((floatAsInt) & BYTESIZE);

        return ret;
    }
}

Related

  1. floatToByteArrayBE(float data)
  2. floatToBytes(final float f)
  3. floatToBytes(float f)
  4. floatToBytes(float fnum, byte[] bytes, int startIndex)
  5. floatToBytes(float num, byte[] arr, int pos)
  6. FloatToBytesLE(final float value)
  7. floatToBytesLE(float f, byte[] bytes, int off)