Convert float To Byte array - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Convert float To Byte array

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        float f = 2.45678f;
        System.out.println(java.util.Arrays.toString(floatToByte(f)));
    }//  w ww  . j a  v a 2 s.  c o  m

    public static byte[] floatToByte(float f) {
        return toLH(Float.floatToRawIntBits(f));
    }

    public static byte[] toLH(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }
}

Related Tutorials