开发者

Handling Signed Binary Data in Haskell without unsafeCoerce

I'm reading in a struct from a binary file that contains signed 16bit integers using the Get monad from Data.Binary. My current code looks like:

data DetectorStats = DetectorStats Int16 Word8 Word8
                     Word8 Int16 Version Int16 
                     deriving Show

getDetectorStats :: Get DetectorStats
getDetectorStats = do
  productNumber <- getWord16be
  bitPerCoordinate <- getWord8
  energyCapability <- getWord8
  timingCapabilit开发者_运维百科y <- getWord8
  clockFrequency <- getWord16be
  serialNumber <- getWord16be
  return (DetectorStats (unsafeCoerce productNumber )
                        bitPerCoordinate
                        energyCapability 
                        timingCapability
                        (unsafeCoerce clockFrequency)
                        firmwareVersion
                        (unsafeCoerce serialNumber))

I'm not happy about using unsafeCoerce, but there doesn't appear to be a way to read in an Int16 directly, nor a way to convert the Word16 into an Int16. Is there a better way of handling this?


fromIntegral will convert Word16 to Int16. However you must check that it gets the result you anticipate vis-a-vis signing.


The Data.Convertible package should do what you're asking for.

For instance to convert from Word16 to Int16:

> (convert (6 :: Word16)) :: Int16
6


Building on Stephen's answer here is an implementation the get and put functions for Int8, Int16 and Int32 analogious to the existing ones for Word8, Word16 and Word32. I haven't as yet required Int64 or Host-endian support but these can be added:

{-# LANGUAGE RecordWildCards #-}
module GetAndPutForInt
( getInt8
, getInt16be
, getInt16le
, getInt32be
, getInt32le
, putInt8
, putInt16be
, putInt16le
, putInt32be
, putInt32le
) where

import Data.Binary
import Data.Binary.Get
import Data.Binary.Put

import Data.Int
import Data.Word

import qualified Data.ByteString.Lazy as B

getInt8 :: Get Int8
getInt8 = do a <- getWord8
             return $ fromIntegral a
getInt16be :: Get Int16
getInt16be = do a <- getWord16be
                return $ fromIntegral a
getInt16le :: Get Int16
getInt16le = do a <- getWord16le
                return $ fromIntegral a
getInt32be :: Get Int32
getInt32be = do a <- getWord32be
                return $ fromIntegral a
getInt32le :: Get Int32
getInt32le = do a <- getWord32le
                return $ fromIntegral a

putInt8 :: Int8 -> Put
putInt8 i = putWord8 ((fromIntegral i) :: Word8)
putInt16be :: Int16 -> Put
putInt16be i = putWord16be ((fromIntegral i) :: Word16)
putInt16le :: Int16 -> Put
putInt16le i = putWord16le ((fromIntegral i) :: Word16)
putInt32be :: Int32 -> Put
putInt32be i = putWord32be ((fromIntegral i) :: Word32)
putInt32le :: Int32 -> Put
putInt32le i = putWord32le ((fromIntegral i) :: Word32)

data TestType = TestType
    { a :: Int16
    , b :: Int16
    } deriving (Show, Eq)

instance Binary TestType where
    put TestType{..} =
      do putInt16be a
         putInt16le b
    get = do a <- getInt16be
             b <- getInt16le
             return TestType{..}

main :: IO ()
main = do
    putStrLn "Supplies Get and Put support to Int8, Int16 etc. types as Data.Binary.Get and Data.Binary.Push do for Word8, Word 16 etc."
    putStrLn ""
    putStrLn "Test data in bytes:"
    print bytes
    putStrLn ""
    putStrLn "As TestType:"
    print (decode bytes :: TestType)
    putStrLn ""
    putStrLn "Back to bytes:"
    print $ (encode ((decode bytes) :: TestType))
  where
    bytes = B.pack $ concat $ replicate 2 [0xCD,0xEF]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜