{- Convert big-endian hexadecimal to big-endian decimal with zero-padding so that the decimal may be losslessly converted back to data. -} {- Copyright 2015 Ken Takusagawa This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . -} {-# LANGUAGE ScopedTypeVariables, LambdaCase #-} module Main(main) where { import Data.List; import Data.Text.Read(hexadecimal); import qualified Data.Text as Text; main :: IO(); main = getContents >>= mapM_ putStrLn . map (to_dec_padded . head . words) . lines; -- designed for a pipe of input from, say, sha1sum read_hex :: String -> Integer; read_hex s = case hexadecimal $ Text.pack s of { Left string -> error string; Right (i,remainder) -> if Text.null remainder then i else error "leftover text"; }; equivalent_decimal_length :: Integer -> Integer; equivalent_decimal_length hex_length = genericLength $ show $ read_hex $ genericReplicate hex_length 'f'; to_dec_padded :: String -> String; to_dec_padded s = reverse $ genericTake (equivalent_decimal_length $ genericLength s) $ (reverse $ show $ read_hex s) ++ repeat '0'; } --end