Non-decimal radices/Convert: Difference between revisions
Content added Content deleted
No edit summary |
m (→{{header|Haskell}}: Minor tidying) |
||
Line 1,378: | Line 1,378: | ||
If we want to assume a default character set, then a general '''toBase''' (Int -> Int -> String) can be also be derived from '''inBaseDigits'''. |
If we want to assume a default character set, then a general '''toBase''' (Int -> Int -> String) can be also be derived from '''inBaseDigits'''. |
||
<lang haskell>import Data. |
<lang haskell>import Data.Char (intToDigit) |
||
import Data. |
import Data.List (unfoldr) |
||
inBaseDigits :: |
inBaseDigits :: String -> Int -> String |
||
inBaseDigits ds n = |
inBaseDigits ds n = reverse $ unfoldr go n |
||
where |
|||
base = length ds |
|||
in reverse $ |
|||
go x |
|||
| 0 < x = |
|||
let (d, r) = quotRem x base |
|||
in Just (ds !! r, d) |
|||
⚫ | |||
in Just (ds !! r, d) |
|||
⚫ | |||
n |
|||
inLowerHex :: Int -> String |
inLowerHex :: Int -> String |
||
Line 1,412: | Line 1,410: | ||
toBase :: Int -> Int -> String |
toBase :: Int -> Int -> String |
||
toBase intBase n |
toBase intBase n |
||
| (intBase < 36) && (intBase > 0) = |
|||
inBaseDigits (take intBase (['0' .. '9'] ++ ['a' .. 'z'])) n |
|||
| otherwise = [] |
|||
main :: IO () |
main :: IO () |