Java Convert via ByteBuffer toBytes(final float val)

Here you can find the source of toBytes(final float val)

Description

Convert a float value to a byte array

License

Apache License

Parameter

Parameter Description
val a parameter

Return

the byte array

Declaration

public static byte[] toBytes(final float val) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;

public class Main {
    private static final int SIZEOF_FLOAT = Float.SIZE / Byte.SIZE;

    /**//from  w w  w . j av a2s. c  o  m
     * Convert a float value to a byte array
     * @param val
     * @return the byte array
     */
    public static byte[] toBytes(final float val) {
        ByteBuffer bb = ByteBuffer.allocate(SIZEOF_FLOAT);
        bb.putFloat(val);
        return bb.array();
    }
}

Related

  1. toByteArrayFromLong(long longValue)
  2. toBytes(BigDecimal number, int byteLength)
  3. toBytes(char[] ch)
  4. toBytes(char[] chars)
  5. toBytes(char[] string)
  6. toBytes(final UUID uuid)
  7. toBytes(InputStream input)
  8. toBytes(int value)
  9. toBytes(long l)