基於Stephen的答案建立在這裏是一個實現Int8,Int16和Int32的獲取和放置函數,類似於現有的Word8,Word16和Word32。我至今還未要求Int64
或主機端的支持,但這些可以添加:
{-# 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]
對我很好:'fromIntegral(maxBound - 4 :: Word16):: Int16'''> -5'。 – sclv 2011-03-01 22:34:42
該文檔說:「從一體轉換WOrd到字節類型的代表,而不是價值」 – fuz 2011-03-02 12:48:48