[Haskell-cafe] Just for a laugh...
David Roundy
droundy at darcs.net
Thu May 31 19:09:42 EDT 2007
On Thu, May 31, 2007 at 11:36:54PM +0200, Tomasz Zielonka wrote:
> You can imitate the C++ code using the FFI libraries:
>
> import Foreign.Storable
> import Foreign
> import Data.Word
> import Data.Bits
>
> getDoubleBits :: Double -> IO String
> getDoubleBits d = alloca $ \ptr -> do
> poke ptr d
> bs <- peekArray (sizeOf d) (castPtr ptr :: Ptr Word8)
> return . concatMap (concatMap (show . fromEnum) . flip map [7,6..0] . testBit) $ bs
>
> (I'm not sure this code prints bits in the right order).
> You can generalize this to
> getStorableBits :: Storable a => a -> IO String
Note also that you can use unsafePerformIO to safely get pure functions
doing both these operations.
--
David Roundy
Department of Physics
Oregon State University
More information about the Haskell-Cafe
mailing list