简体   繁体   中英

Manipulating hexadecimal data in Haskell

I have a CSV file full of logged data which I would like to process in Haskell. The data in the CSV file is in hexadecimal format. When I read it into Haskell I have strings such as "0xFF5FFFC8EC5FFEDF" which represents 8 bytes of data.

To process the data, I would like to convert the string into a data type which will allow me to do bit twiddling (bitwise AND, OR and XOR). Then when I am done I would like to convert the final result back into a hex sting so I can write it to a file.

Is this easy to do in Haskell? Which modules should I be looking at?

You can use read to parse ints or floats. It is in the Prelude so you can use it without any additional modules.

Try:

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

(it gives b = 1.8401707840883393e19)

Also, for parsing CSV, you may aswell make your own functions to do it. I have just a week ago written a simple CSV parser.

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)

I am going to assume that your binary data could be of arbitrary length. Things can be simplified if, for instance, your binary data fits into an Int64 .

I'll recommend the following libraries and modules:

For an example of how to perform bitwise operations on a ByteStrings, have a look at the end of this tutorial at the School of Haskell:

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

For examples of how to use cassava , look at the examples directory of the source repo:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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