繁体   English   中英

在Haskell中处理十六进制数据

[英]Manipulating hexadecimal data in Haskell

我有一个CSV文件,其中记录了所有数据,我想在Haskell中进行处理。 CSV文件中的数据为十六进制格式。 当我将其读入Haskell时,我会看到诸如“ 0xFF5FFFC8EC5FFEDF”之类的字符串,它代表8个字节的数据。

为了处理数据,我想将字符串转换为数据类型,这将允许我进行位旋转(按位AND,OR和XOR)。 然后,当我完成后,我想将最终结果转换回十六进制字符串,以便将其写入文件。

在Haskell这样做容易吗? 我应该看哪个模块?

您可以使用read解析int或float。 它位于Prelude中,因此您可以使用它而无需任何其他模块。

尝试:

a = "0xFF5FFFC8EC5FFEDF"
b = read a::Double

(它给出b = 1.8401707840883393e19)

另外,对于解析CSV,您也可以自己编写函数来执行。 我一周前才写了一个简单的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)

我将假设您的二进制数据可以具有任意长度。 例如,如果您的二进制数据适合Int64 ,则事情可以简化。

我将推荐以下库和模块:

有关如何对ByteStrings执行按位运算的示例,请查看Haskell学校的本教程结尾:

https://www.fpcomplete.com/school/to-infinity-and-beyond/pick-of-the-week/bytestring-bits-and-pieces

有关如何使用cassavaexamples ,请查看源存储库的examples目录:

https://github.com/tibbe/cassava/tree/master/examples

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM