Java Float convert from byte array via ByteBuffer

Description

Java Float convert from byte array via ByteBuffer

import java.nio.ByteBuffer;

public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] b = new byte[] { 4, 5, 6, 7 };
    System.out.println(byteArrayToFloat(b));
  }/*from w w w  .j av a2s  .  co  m*/

  /**
   * Converts a byte array to a float.
   * 
   * @param b
   *          - The byte array. Must be a length of at least 4.
   * @return The float.
   */
  public static float byteArrayToFloat(byte[] b) {
    return ByteBuffer.wrap(b).getFloat();
  }
}



PreviousNext

Related