Java ByteBuffer store float array

Description

Java ByteBuffer store float array


import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class Main {
  public static void main(String[] argv) throws Exception {
    float[] arr = new float[] { 3.4f, 3.5f, 3.6f, 3.7f, 3.8f, 3.1234f, 6.2344f, 8.34f, 9.8f };
    System.out.println(arr2ByteBuffer(arr));
  }// w ww . ja v a2s .com

  public static ByteBuffer arr2ByteBuffer(float[] arr) {

    ByteBuffer ibb = ByteBuffer.allocateDirect(arr.length * 4);
    ibb.order(ByteOrder.nativeOrder());
    FloatBuffer fbb = ibb.asFloatBuffer();
    fbb.put(arr);

    ibb.position(0);
    return ibb;
  }
}



PreviousNext

Related