Convert a string representing a binary number to a base 10 string haskell

前端 未结 6 802
伪装坚强ぢ
伪装坚强ぢ 2021-01-01 18:11

I have the string \"1001\" and I want the string \"9\".

The numeric library has the (rather clunky) showIntAtBase, but I haven\'t been able to find the opposite.

6条回答
  •  无人及你
    2021-01-01 18:47

    This helps? http://pleac.sourceforge.net/pleac_haskell/numbers.html

    from the page:

    bin2dec :: String -> Integer
    bin2dec = foldr (\c s -> s * 2 + c) 0 . reverse . map c2i
        where c2i c = if c == '0' then 0 else 1
    -- bin2dec "0110110" == 54
    

提交回复
热议问题