Binary to decimal conversion

Asked

Viewed 277 times

0

I made an algorithm that converts from Binary to Decimal, now I need to develop one that does the opposite. I’ll leave the code below, but this with error. Thank you.

converte' :: [Int] -> Int
converte' [] = 0
converte' (x:xs) = ((2 ^ comprimento xs) * x) + (2 ^ (comprimento xs-1)) * x

ps: function comprimento measures the tail size of the current list

1 answer

0

You have not posted your function comprimento, so I used the functions length and tail haskell’s.

A possible function to convert decimal to binary:

decParaBin 0 = [0]
decParaBin 1 = [1]
decParaBin x = decParaBin (x `div` 2) ++ [x `mod` 2]

You didn’t post your function that converts binary to decimal, so I also made a:

binParaDec [] = 0
binParaDec x = head x * 2 ^ (length x - 1) + binParaDec (tail x)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.