Converts a float to a byte array with a length of 4. - Java File Path IO

Java examples for File Path IO:Byte Array

Description

Converts a float to a byte array with a length of 4.

Demo Code


//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    public static void main(String[] argv) throws Exception {
        float f = 2.45678f;
        System.out.println(java.util.Arrays.toString(floatToByteArray(f)));
    }//  w  w w . j ava  2 s .co m

    /**
     * Converts a float to a byte array with a length of 4.
     * @param f - The float.
     * @return The byte array.
     */
    public static byte[] floatToByteArray(float f) {
        return ByteBuffer.allocate(4).putFloat(f).array();
    }
}

Related Tutorials