March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
21
Task/CRC-32/Haskell/crc-32-1.hs
Normal file
21
Task/CRC-32/Haskell/crc-32-1.hs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import Data.Bits
|
||||
import Data.Word
|
||||
import Numeric
|
||||
|
||||
crcTable :: Word32 -> Word32
|
||||
crcTable i = table !! (fromIntegral i)
|
||||
where
|
||||
table = map (\a -> iterate xf a !! 8) [0..255]
|
||||
xf r = let d = shiftR r 1 in
|
||||
if r .&. 1 == 1 then xor d 0xedb88320 else d
|
||||
|
||||
charToWord :: Char -> Word32
|
||||
charToWord c = (fromIntegral . fromEnum) c
|
||||
|
||||
calcCrc :: String -> Word32
|
||||
calcCrc text = complement ( foldl cf (complement 0) text )
|
||||
where cf crc x = xor (shiftR crc 8) (crcTable $ xor (crc .&. 0xff) (charToWord x) )
|
||||
|
||||
crc32 text = showHex ( calcCrc text ) ""
|
||||
|
||||
main = putStrLn $ crc32 "The quick brown fox jumps over the lazy dog"
|
||||
13
Task/CRC-32/Haskell/crc-32-2.hs
Normal file
13
Task/CRC-32/Haskell/crc-32-2.hs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import Data.List (genericLength)
|
||||
import Numeric (showHex)
|
||||
import Foreign.C
|
||||
|
||||
foreign import ccall "zlib.h crc32" zlib_crc32 :: CULong -> CString -> CUInt -> CULong
|
||||
|
||||
main = do
|
||||
let s = "The quick brown fox jumps over the lazy dog"
|
||||
ptr <- newCString s
|
||||
|
||||
let r = zlib_crc32 0 ptr (genericLength s)
|
||||
|
||||
putStrLn $ showHex r ""
|
||||
Loading…
Add table
Add a link
Reference in a new issue