How to convert floating point numbers to 4 bytes in Java?
•
Java
I haven't been able to convert such things:
byte[] b = new byte[] { 12,24,19,17};
Become something like this:
float myfloatvalue = ?;
Can someone give me an example?
And how do you turn this float back to bytes?
Solution
From byte [] – > floating, you can:
byte[] b = new byte[] { 12,17}; float myfloatvalue = ByteBuffer.wrap(b).getFloat();
This is using ByteBuffer Allocate is an alternative to converting float – > Byte []:
int bits = Float.floatToIntBits(myFloat); byte[] bytes = new byte[4]; bytes[0] = (byte)(bits & 0xff); bytes[1] = (byte)((bits >> 8) & 0xff); bytes[2] = (byte)((bits >> 16) & 0xff); bytes[3] = (byte)((bits >> 24) & 0xff);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码