我有一个由4个字节组成的数组,代表一个浮点数。 由于Kotlin缺少字节级的按位运算符,我应该如何以最优的方式将它转换为浮点数?
我有一个由4个字节组成的数组,代表一个浮点数。 由于Kotlin缺少字节级的按位运算符,我应该如何以最优的方式将它转换为浮点数?
ByteBuffer
,它具有getFloat()
和getFloat(index)
函数来实现这一点:val bytes = byteArrayOf(1, 2, 3, 4)
val buffer = ByteBuffer.wrap(bytes)
val float1 = buffer.getFloat() // Uses current position and increments it by 4
val float2 = buffer.getFloat(0) // Uses specified position
.order(ByteOrder.LITTLE_ENDIAN)
:
val wrap = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN)
- moobifloat.toBits()
,然后使用类似以下的代码:new byte[] { (byte) (intBits >> 24), (byte) (intBits >> 16), (byte) (intBits >> 8), (byte) (intBits) }
。 - SaPropper