如何将4字节数组转换为java中的float [英] How to convert 4 bytes array to float in java

查看:310
本文介绍了如何将4字节数组转换为java中的float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个4字节数组,表示一个浮点值。从网络中读取字节,例如3e 3f e3 a0。在Java中,如何将它从byte []转换为float?

解决方案 / code>是16位。如果你意味着你有一个4 byte 值以小端字节顺序,你需要转换为 float ,你可以使用 ByteBuffer

  byte [] bytes = {} 
float f = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getFloat();


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?

解决方案

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

这篇关于如何将4字节数组转换为java中的float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆