Converting ByteArrays to primitives (especially Float / Byte) on Multiplatform

Hi guys,

I’m not sure how to do this:

I’m getting ByteArrays as inputs, and they contain the bytes that represent big-endian, signed primitives: Byte, Short, Int, Long, Float, Double. And what I want to achieve is to convert those ByteArrays into the corresponding Kotlin primitives, in a way that works for JVM, JS and Native targets.

So far, I’m using the regular SHL/OR procedure for converting the integral types, but I’m a bit stuck on how to convert 32-bit/64-bit IEEE 754-2008 floating-point ByteArrays into Kotlins Float/Double datatypes, respectively.

An idea that came to my mind was to just cast the ByteArrays directly to the desired datatype. But would that actually work for all targets? And how would it behave concerning the endianness?

Basically, my question is whether there is a generally accepted “standard” way to convert ByteArrays to Kotlin primitives in a platform-independent way, and if not, how you’d do it only for Float/Double.

Thank you!

submitted by /u/Raph0007
[link] [comments]