Java Float Array to Byte Array floatArrayToBytes(float[] d)

Here you can find the source of floatArrayToBytes(float[] d)

Description

float Array To Bytes

License

Open Source License

Declaration

public static byte[] floatArrayToBytes(float[] d) 

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 {
    public static byte[] floatArrayToBytes(float[] d) {
        byte[] r = new byte[d.length * 4];
        for (int i = 0; i < d.length; i++) {
            byte[] s = floatToBytes(d[i]);
            for (int j = 0; j < 4; j++)
                r[4 * i + j] = s[j];/* w  w  w .ja v  a  2 s  .c om*/
        }
        return r;
    }

    public static byte[] floatToBytes(float d) {
        int i = Float.floatToRawIntBits(d);
        return intToBytes(i);
    }

    public static byte[] intToBytes(int v) {
        byte[] r = new byte[4];
        for (int i = 0; i < 4; i++) {
            r[i] = (byte) ((v >>> (i * 8)) & 0xFF);
        }
        return r;
    }
}

Related

  1. floats2bytesBE(float[] val)