Converts a byte array to a float via ByteBuffer. - Java java.nio

Java examples for java.nio:ByteBuffer Convert

Description

Converts a byte array to a float via ByteBuffer.

Demo Code


//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] b = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(byteArrayToFloat(b));
    }/*  w w  w .  ja v a  2s .c  om*/

    /**
     * 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();
    }
}

Related Tutorials