Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Bytestring to Float Array

Hi have binaries of float data (single-precision 32-bit IEEE) that I would like to work on. How can I best load this for further use, ideally as (IOArray Int Float).

bytesToFloats :: ByteString -> [Float]
bytesToFloatArray :: ByteString -> IOArray Int Float
like image 684
Fabian Barkhau Avatar asked Mar 21 '23 03:03

Fabian Barkhau


1 Answers

If you've got bog standard single-precision floats, and you just want to work them over in Haskell, you can always be down and dirty about it:

import Data.ByteString.Internal as BS
import qualified Data.Vector.Storable as V

bytesToFloats :: BS.ByteString -> V.Vector Float
bytesToFloats = V.unsafeCast . aux . BS.toForeignPtr
  where aux (fp,offset,len) = V.unsafeFromForeignPtr fp offset len
like image 142
Anthony Avatar answered Mar 31 '23 17:03

Anthony