Haskell code: Simple 8-bit Checksum
module Main where import System (getArgs) import Data.Char (ord, digitToInt) import Data.Bits ((.&.)) import Numeric (showHex) -- command line helper to calculate the CIMD checksum on the bytes entered: -- CHECKSUM: all bytes are added and only the low byte is maintained -- for the result, i.e. sum all characters modulo 256. -- main :: IO () main = do args <- getArgs let values = map cliToInt args let chksum = foldl (\bv acc -> (.&.) 255 $ bv + acc) 0 values putStrLn $ "Checksum: " ++ (showHex chksum " Hex") ++ (", Decimal: " ++ show chksum) -- characters are processed according to their length: -- -- 3 characters: assumed to be STX, ETX or TAB and translated to 2, 3 or 9 -- 1 character: assumed to be ASCII and treated as such -- 2 characters: assumed to be HI-LO hex eg FA would be converted to 240 -- cliToInt :: String -> Int cliToInt text = case length text of 1 -> ord $ text !! 0 2 -> 16 * (digitToInt $ text!!0) + (digitToInt $ text!!1) 3 -> case text of "stx" -> 2 "etx" -> 3 "tab" -> 9 _ -> error ("Invalid 3-code: " ++ text) _ -> error ("Only use 1, 2 or 3 length values: " ++ text)