
I have written a code to convert hexadecimal numbers into base 10 number but its working only for 32 bits and I need it to work upto 160 bits , code is given below. please help. --its a program to convert hexadecimal numbers into base 10 numbers import Char -- the function "func" takes a list of strings in hexadecimal and convert -- it to a list of corresponding numbers in decimal format -- func "eefba12" = [14,14,15,11,10,1,2] func [] = [] func (x:xs) |(ord x > 57) = ((ord x)-87):func xs |otherwise = ((ord x)-48):func xs -- (mul.func) function takes a hexadecimal number and convert it to base 10 but its working only for 32 bits -- whereas haskell supports calculations much higher than 32 bits..... -- mul.func $ "ee" = 238 -- mul.func $ "e23e" = 57918 mul xs = mul1 xs ((length xs)-1) mul1 (x:xs) 0 = x mul1 (x:xs) n = (x*16^n)+ mul1 xs (n-1)

ashutosh dimri wrote:
func [] = [] func (x:xs) |(ord x > 57) = ((ord x)-87):func xs |otherwise = ((ord x)-48):func xs
Your problem is with the type of 'func'. *Main> :t func func :: [Char] -> [Int] 'Int' is a 32-bit type, on most systems. The thing that has forced your type to Int is your use of ord, because ord is defined to produce Ints: *Main> :t ord ord :: Char -> Int We can use 'fromIntegral' to convert an Int to any other numeric type: func [] = [] func (x:xs) |(ord x > 57) = ((fromIntegral.ord $ x)-87):func xs |otherwise = ((fromIntegral.ord $ x)-48):func xs And now 'func' has the more general type: *Main> :t func func :: (Num a) => [Char] -> [a] (i.e. func works on any numeric type 'a'). and now it works for you on very large values: *Main> mul.func $ "eeeeeeeeee" 1026210852590 Hope that helps, Jules
participants (2)
-
ashutosh dimri
-
Jules Bean