Return to Snippet

Revision: 47084
at June 1, 2011 00:30 by rtperson


Initial Code
import qualified Data.ByteString.Lazy.Char8 as BS

convertFromDecimal :: Int -> Int -> BS.ByteString -> BS.ByteString
convertFromDecimal num toBase accum
    | toBase < 0 = error "base must be greater than zero"
    | toBase > 20 = error "base must be <= 20"
    | num < 0     = error "number cannot be negative"
    | num <= 0 = accum
    | num > 0  = 
        let chars  = "0123456789ABCDEFGHIJ"
            over   = num `mod` toBase
            remain = num `div` toBase
            accum' = BS.cons (chars !! over) accum
        in 
            convertFromDecimal remain toBase accum'

Initial URL

                                

Initial Description
Another radix conversion, when you need to zippy greatness of Haskell's ByteString.(Haskell strings are slooooow!)

TODO: rewrite this using overloaded strings.

Initial Title
Haskell Radix Conversion using ByteStrings

Initial Tags

                                

Initial Language
Haskell