Вы можете использовать read
для синтаксического анализа или плавания. Это в Prelude, поэтому вы можете использовать его без каких-либо дополнительных модулей.
Try:
a = "0xFF5FFFC8EC5FFEDF"
b = read a::Double
(это дает Ь = 1.8401707840883393e19)
Кроме того, для разбора CSV, вы можете Aswell сделать свои собственные функции, чтобы сделать это. У меня всего неделю назад был написан простой синтаксический анализатор CSV.
module CSVUtils
(parseCSV, showCSV
, readCSV , writeCSV
, colFields
, Separator, Document
, CSV , Entry
, Field
)
where
import Data.Char
import Data.List
{-
A simple utility for working with CSV (comma-separated value) files. These
are simple textual files where fields are delimited with a character (usually a comma
or a semicolon). It is required that the CSV document is well-formed, i.e., that
it contains an equal number of fields per row.
-}
type Separator = String
type Document = String
type CSV = [Entry]
type Entry = [Field]
type Field = String
doc = "John;Doe;15\nTom;Sawyer;12\nAnnie;Blake;20"
brokenDoc = "One;Two\nThree;Four;Five"
{-
(a) Takes a separator and a string representing a CSV document and returns a
CSV representation of the document.
-}
-- !! In the homework text is said Separator is going to be Char and now the type is String
-- !! so I'm just going to take head
parseCSV :: Separator -> Document -> CSV
parseCSV sep doc
| (head sep) `notElem` doc = error $ "The character '"++sep++"' does not occur in the text"
| 1 /= length (nub (map length (lines doc))) = error $ "The CSV file is not well-formed"
| otherwise = [splitOn sep wrd | wrd <- lines doc ]
{-
(b) Takes a separator and a CSV representation of
a document and creates a CSV string from it.
-}
showCSV :: Separator -> CSV -> Document
showCSV sep = init . unlines . map (intercalate sep)
{-
(c) Takes a CSV document and a field number
and returns a list of fields in that column.
-}
colFields :: Int -> CSV -> [Field]
colFields n csv = [ if length field > n
then field !! n
else error $ "There is no column "++(show n)++" in the CSV document"
| field <- csv]
{-
(d) Takes a file path and a separator and returns the CSV representation of the file.
-}
readCSV :: Separator -> FilePath -> IO CSV
readCSV sep path = do
file <- readFile path
return $ parseCSV sep file
{-
(e) Takes a separator, a file path, and a CSV document and writes the document into a file.
The return type of writeCSV is a special case of IO { we need to wrap an impure
action, but do not actually have to return anything when writing. Thus, we
introduce(), or the unit type, which holds no information (consider it a 0-
tuple).
-}
writeCSV :: Separator -> FilePath -> CSV -> IO()
writeCSV sep path csv = writeFile path (showCSV sep csv)
Как долго ваши шестнадцатеричные строки? Будут ли они всегда вписываться в 64-битный int, или они могут быть длиннее? – ErikR