How to convert 4 bytes array to float in java

前端 未结 2 685
独厮守ぢ
独厮守ぢ 2020-12-08 14:16

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条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 14:39

    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();
    

提交回复
热议问题