将 float[] 转换为 byte[] 再次转换为 float[]
所以我在这里想做的是获取一个 float[] ,将其转换为 byte[] ,将其作为数据报包通过网络发送,然后将其转换返回到接收终端的byte[]
。
现在我知道我可以使用 getBytes[]
方法将 float[]
转换为 byte[]
。但我不知道如何反转转换。
So what I'm trying to do here is get a float[]
, convert it to byte[]
, send it through the network as a datagram packet and then convert it back to a byte[]
at the receiving terminal.
Now I know I can convert float[]
to byte[]
by using the getBytes[]
method. But I don't know how to reverse the conversion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我认为您想使用 ByteBuffer 类,它具有 putFloat 和 getFloat 方法。
I think you want to make use of the
ByteBuffer
class, which hasputFloat
andgetFloat
methods.另一种方式...使用 ByteArrayOutputStream/DataOutputStream 进行输出
使用 ByteArrayInputStream/DataInputStream 进行输入
Another way... use ByteArrayOutputStream/DataOutputStream for output
Use ByteArrayInputStream/DataInputStream for input
对 Steven Chou 答案的改进
An improvement to Steven Chou's answer
使用
Float.floatToIntBits()
将浮点的位值提取为整数,然后使用BigInteger.toByteArray()
生成byte[]< /代码>。可以使用采用
byte[]
参数的BigInteger
构造函数,然后使用Float.intBitsToFloat()
来逆转这一情况。Use
Float.floatToIntBits()
to extract the bit-value of the float as an integer, then useBigInteger.toByteArray()
to make abyte[]
. This can be reversed using theBigInteger
constructor that takes abyte[]
argument, and thenFloat.intBitsToFloat()
.这对我以后的参考比什么都重要。
可以简化为一行,例如 https://stackoverflow.com/a/44104399/322017。
This is more for my future reference than anything else.
Can be reduced to a single line like https://stackoverflow.com/a/44104399/322017.
发件人:
收件人:
Sender:
Receiver:
需要在ByteBuffer的FloatBuffer中使用getFloat()和putFloat()命令。事实上,您当然应该这样做,因为速度太快了。理解字节操作是一件很棒的事情。您还可以混合和匹配数据,并根据需要放置和获取数据。所有这些都由字节缓冲区支持。因此,发送数组的常见情况是,您还需要发送数组的大小。
您只需确保分配足够的字节来存储缓冲区中的所有内容即可。写一些东西,写一些其他东西等等。理解这一点会让事情变得更容易。另一方面,我们基本上做同样的事情,尽管我们需要额外的读取,因为我们不知道数组有多大,只是知道有一个:
对于完整的功能,尽管不完全是其中的一部分:
You need to use the getFloat() and putFloat() commands in the FloatBuffer of the ByteBuffer. In fact, you should certainly do this simply because of the sheer speed. And it's a great thing to understand for byte manipulations. You can also mix and match the data, and put and get it as needed. All of it is backed by the byte buffer. So the common happening of sending an array, you need to send the size of the array too.
You just make sure to allocate enough bytes to store everything in the buffer. Write some stuff, write some other stuff etc. Understanding this point makes things way easier. On the flipside we do basically the same thing, though we require an additional read because we don't know how big the array is, just that there is one:
And for full functionality, though not exactly part of this:
ByteBuffer 文档: https://docs.oracle.com /javase/7/docs/api/java/nio/ByteBuffer.html
如果您的应用程序对性能更加敏感,那么您可以考虑这种解决方案,而无需创建许多对象:
ByteBuffer document: https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html
If your application is more performance sensitive, then you can consider this solution without creating many objects:
Byte 类有一个方法 floatValue();
http://docs .oracle.com/javase/1.4.2/docs/api/java/lang/Byte.html#floatValue%28%29
Byte class has a method floatValue();
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Byte.html#floatValue%28%29