java – Float to Byte Array Conversion

Question:

How can I be converting from float or integer (if that's possible) to a byte array in Java.

Let's say:

float x=180

Theoretically you would need to create 1 byte array with 2 positions. Is there any way to 'automate' this?

Answer:

ByteBuffer.allocate(4).putFloat(180).array();

I put it on GitHub for future reference .

This would create an array with 4 positions. Bytes alone won't do much.

Documentation . There it shows the methods for dealing with the other types you might want. What this class does is just treat the data as if they were just bytes.

Scroll to Top