How to print integer literals in binary or hex in haskell?

前端 未结 8 1325
说谎
说谎 2021-01-30 02:13

How to print integer literals in binary or hex in haskell?

printBinary 5 => \"0101\"

printHex 5 => \"05\"

Which libraries/functions allo

8条回答
  •  不要未来只要你来
    2021-01-30 03:02

    You can convert integer to binary with something like the following:

    decToBin x = reverse $ decToBin' x
      where
        decToBin' 0 = []
        decToBin' y = let (a,b) = quotRem y 2 in [b] ++ decToBin' a
    

    usage in GHCi:

    Prelude> decToBin 10
    [1,0,1,0]
    

提交回复
热议问题