How to convert 4 bytes array to float in java

2019-01-22 11:54发布

I have a 4 bytes array which represent a float value. The bytes is read from network, for example, 3e 3f e3 a0. How can I convert it from byte[] to float in java?

2条回答
相关推荐>>
2楼-- · 2019-01-22 12:34

In Java a char is 16-bit. If you mean you have a 4 byte values in little endian byte order which you need to convert to a float you can use ByteBuffer.

byte[] bytes = { }
float f = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getFloat();
查看更多
Deceive 欺骗
3楼-- · 2019-01-22 12:41

Try this:

float foo = Float.intBitsToFloat( buffer[n] ^ buffer[n+1]<<8 ^ buffer[n+2]<<16 ^ buffer[n+3]<<24 );

查看更多
登录 后发表回答