我正在尝试使用 Haskell 解析二进制格式(PES):
import qualified Data.ByteString.Lazy as BL
import Data.Word
import Data.Word.Word24
import qualified Data.ByteString.Lazy.Char8 as L8
data Stitch = MyCoord Int Int deriving (Eq, Show)
data PESFile = PESFile {
pecstart :: Word24
, width :: Int
, height :: Int
, numColors :: Int
, header :: String
, stitches :: [Stitch]
} deriving (Eq, Show)
readPES :: BL.ByteString -> Maybe PESFile
readPES bs =
let s = L8.drop 7 bs
pecstart = L8.readInt s in
case pecstart of
Nothing -> Nothing
Just (offset,rest) -> Just (PESFile offset 1 1 1 "#PES" [])
main = do
input <- BL.getContents
print $ readPES input
我需要读取 pecstart 以获取其他数据的偏移量(宽度、高度和针迹)但这对我不起作用,因为我需要读取 24 位值,而 ByteString 包似乎没有 24位版本。
我应该使用不同的方法吗?Data.Binary 包似乎适用于简单格式,但我不确定它如何用于这样的事情,因为您必须读取一个值才能找到文件中其他数据的偏移量。我错过了什么?