bit manipulation - Manipulating hexadecimal data in Haskell -
i have csv file full of logged data process in haskell. data in csv file in hexadecimal format. when read haskell have strings such "0xff5fffc8ec5ffedf" represents 8 bytes of data.
to process data, convert string data type allow me bit twiddling (bitwise and, or , xor). when done convert final result hex sting can write file.
is easy in haskell? modules should looking at?
you can use read
parse ints or floats. in prelude can use without additional modules.
try:
a = "0xff5fffc8ec5ffedf" b = read a::double
(it gives b = 1.8401707840883393e19)
also, parsing csv, may aswell make own functions it. have week ago written simple csv parser.
module csvutils ( parsecsv, showcsv , readcsv , writecsv , colfields , separator, document , csv , entry , field ) import data.char import data.list {- simple utility working csv (comma-separated value) files. these simple textual files fields delimited character (usually comma or semicolon). required csv document well-formed, i.e., contains 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 separator , string representing csv document , returns csv representation of document. -} -- !! in homework text said separator going char , type string -- !! i'm going take head parsecsv :: separator -> document -> csv parsecsv sep doc | (head sep) `notelem` doc = error $ "the character '"++sep++"' not occur in text" | 1 /= length ( nub ( map length (lines doc))) = error $ "the csv file not well-formed" | otherwise = [spliton sep wrd | wrd <- lines doc ] {- (b) takes separator , csv representation of document , creates csv string it. -} showcsv :: separator -> csv -> document showcsv sep = init . unlines . map (intercalate sep) {- (c) takes csv document , field number , returns list of fields in column. -} colfields :: int -> csv -> [field] colfields n csv = [ if length field > n field !! n else error $ "there no column "++(show n)++" in csv document" | field <- csv] {- (d) takes file path , separator , returns csv representation of file. -} readcsv :: separator -> filepath -> io csv readcsv sep path = file <- readfile path return $ parsecsv sep file {- (e) takes separator, file path, , csv document , writes document file. return type of writecsv special case of io { need wrap impure action, not have return when writing. thus, introduce (), or unit type, holds no information (consider 0- tuple). -} writecsv :: separator -> filepath -> csv -> io () writecsv sep path csv = writefile path (showcsv sep csv)
Comments
Post a Comment