I'm looking for a way to convert an array of UInt8 to Float. [27,227] (Which should be something around 95.0) There are a few tips out there to convert to Int but none of these seem to work for Float.
Asked
Active
Viewed 485 times
0
-
1Roughly this: **1)** pad your `UInt8` arrays appropriately so that there's 4 `UInt8`s per float (`[27, 227]` itself isn't valid, because that's only specifying 2 of the 4 bytes that constitute a `Float`) Make sure to specify whether your bytes are in little-endian or big-endian order. **2)** Convert your array into `Data`, **3)** use one of the many snippets on StackOverflow to load that `Data` into a `[Float]`. – Alexander Jun 09 '20 at 19:42
-
There are multiple ways that arrays of `UInt8` might represent numbers. They might be the raw bytes that encoding floating-point numbers in an IEEE-754 format or other format. They might be integers from 0 to 255 that directly represent numbers 0 to 255, so they are converted to floating-point just with those values. They might be integers from 0 to 255 that are fixed-point (scaled) representations of numbers from 0 to 1. Both 255 and 256 are not uncommon scalings for that. Your sample does not seem to help identify any of these. You must supply more information. – Eric Postpischil Jun 09 '20 at 20:18
1 Answers
-1
The simplest solution would be creating a new array of type Float, looping to the Int16 Array and append the converted Float value to the new array for each loop circle.
I haven't heard of a function in the Foundation Library that provides this logic for you.

christophriepe
- 1,157
- 12
- 47