Issue
I have a 4 bytes array which represent a float value. Since kotlin lack of bitwise operations for Byte how can I convert it to float in most optimal way?
Solution
You can use the Java NIO ByteBuffer
, it has the getFloat()
and getFloat(index)
functions for that:
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
Answered By - hotkey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.