Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a float into a byte array and vice versa?

Tags:

java

I am trying to convert a float into a primitive byte[] and vice versa:

public byte[] floatToByteArray(final float value)
{
    return new byte[]
    {
        (byte) (value >> 56),
        (byte) (value >> 48),
        (byte) (value >> 40),
        (byte) (value >> 32),
        (byte) (value >> 24),
        (byte) (value >> 16),
        (byte) (value >> 8),
        (byte) (value)
    };
}

Oddly enough, when I try to shift the newly allocated byte[] back into a float, the result is nothing but rubbish.

However, it would appear as if the same algorithm works just fine when I use the primitive long data type as argument instead.

public byte[] longToByteArray(final long value)
{
    return new byte[]
    {
        (byte) (value >> 56),
        (byte) (value >> 48),
        (byte) (value >> 40),
        (byte) (value >> 32),
        (byte) (value >> 24),
        (byte) (value >> 16),
        (byte) (value >> 8),
        (byte) (value)
    };
}
like image 946
bantini Avatar asked Jan 31 '13 06:01

bantini


People also ask

Can we convert float to byte?

Float to Byte Array Conversion As we know, the size of a float in Java is 32 bit which is similar to an int. So we can use floatToIntBits or floatToRawIntBits functions available in the Float class of Java. And then shift the bits to return a byte array. Click here to learn more about bit shifting operations.

Can we convert float to byte in Java?

byteValue() is a built-in method in Java that returns the value of this Float as a byte(by casting to a byte). Basically it used for narrowing primitive conversion of Float type to a byte value.

How do you convert a float to a whole number?

Method 1: Conversion using int(): To convert a float value to int we make use of the built-in int() function, this function trims the values after the decimal point and returns only the integer/whole number part. Example 1: Number of type float is converted to a result of type int.

How do you turn an object into a Bytearray?

Write the contents of the object to the output stream using the writeObject() method of the ObjectOutputStream class. Flush the contents to the stream using the flush() method. Finally, convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.


2 Answers

Use these instead.

public static byte [] long2ByteArray (long value)
{
    return ByteBuffer.allocate(8).putLong(value).array();
}

public static byte [] float2ByteArray (float value)
{  
     return ByteBuffer.allocate(4).putFloat(value).array();
}
like image 174
shazin Avatar answered Oct 06 '22 16:10

shazin


Just to add another useful one based on @shazin solution. Convert a float array into a byte array:

public static byte[] FloatArray2ByteArray(float[] values){
    ByteBuffer buffer = ByteBuffer.allocate(4 * values.length);

    for (float value : values){
        buffer.putFloat(value);
    }

    return buffer.array();
}
like image 37
Daniel Conde Marin Avatar answered Oct 06 '22 15:10

Daniel Conde Marin