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

前端 未结 8 1186
说谎
说谎 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 02:59

    If you import the Numeric and Data.Char modules, you can do this:

    showIntAtBase 2 intToDigit 10 "" => "1010"
    showIntAtBase 16 intToDigit 1023 "" => "3ff"
    

    This will work for any bases up to 16, since this is all that intToDigit works for. The reason for the extra empty string argument in the examples above is that showIntAtBase returns a function of type ShowS, which will concatenate the display representation onto an existing string.

提交回复
热议问题