Java Convert via ByteBuffer toByteArray(float[] floatArray)

Here you can find the source of toByteArray(float[] floatArray)

Description

to Byte Array

License

Open Source License

Declaration

public static byte[] toByteArray(float[] floatArray) 

Method Source Code


//package com.java2s;
/*/*from  ww  w. ja v  a 2  s  .  c  om*/
 * #%L
 * Ice-9 Tickerplant - Server
 * %%
 * Copyright (C) 2014 - 2015 Snowfall Systems, Inc.
 * %%
 * This file is part of PortfolioEffect Quant Client.
 * 
 * PortfolioEffect Quant Client 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.
 * 
 * PortfolioEffect Quant Client 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 PortfolioEffect Quant Client. If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import java.nio.ByteBuffer;

public class Main {
    public static byte[] toByteArray(float[] floatArray) {
        int times = Float.SIZE / Byte.SIZE;
        byte[] bytes = new byte[floatArray.length * times];
        for (int i = 0; i < floatArray.length; i++) {
            ByteBuffer.wrap(bytes, i * times, times).putFloat(floatArray[i]);
        }
        return bytes;
    }

    public static byte[] toByteArray(double[] doubleArray) {
        int times = Double.SIZE / Byte.SIZE;
        byte[] bytes = new byte[doubleArray.length * times];
        for (int i = 0; i < doubleArray.length; i++) {
            ByteBuffer.wrap(bytes, i * times, times).putDouble(doubleArray[i]);
        }
        return bytes;
    }

    public static byte[] toByteArray(int[] intArray) {
        int times = Integer.SIZE / Byte.SIZE;
        byte[] bytes = new byte[intArray.length * times];
        for (int i = 0; i < intArray.length; i++) {
            ByteBuffer.wrap(bytes, i * times, times).putInt(intArray[i]);
        }
        return bytes;
    }

    public static byte[] toByteArray(long[] longArray) {
        int times = Long.SIZE / Byte.SIZE;
        byte[] bytes = new byte[longArray.length * times];
        for (int i = 0; i < longArray.length; i++) {
            ByteBuffer.wrap(bytes, i * times, times).putLong(longArray[i]);
        }
        return bytes;
    }
}

Related

  1. toByte(int input, int count)
  2. toByteArray(BitSet bits)
  3. toByteArray(char[] chars)
  4. toByteArray(double value)
  5. toByteArray(double[] doubleArray)
  6. toByteArray(InputStream input, boolean nioCopy)
  7. toByteArray(int i)
  8. toByteArray(int integer)
  9. toByteArray(int value)