OK, I have a situation....I have a little PIC microchip sending out 24 bit ADC values over TCP/IP... I want to know what is the nicest / cleanest / best / efficient / geekiest etc way tyo convert the three bytes it sends into an unsigned 24-bit number...
main :: IO ()
main = do
h <- connectTo "picbox" (PortNumber 20000)
len <- liftM unpack $ BS.hGet h 3
hClose h
print len
that currently gives me [17,0,0] on the console, which is 0x000011 in hex.
I have played with ByteString foldl' and all sorts of other ways to do what I want, which is essentially the sum of b0 + (b1*256) + (b2 * 256 * 256) where b0 is 17 and b2 and b3 are the zeros.
I've tried toying with zipping it with an array containing [1, 256, 256*256], I've tried using left and right folds and pre-multiplying the accumulator by 256 each time but the ByteString seems limited to 0-255 and so it doesn't work. I've tried brute force arithmetic by extracting each part and doing the maths etc.
The challenge then is to show me how to deal with ByteStrings and "fromIntegral" type conversions; I am still learning Haskell and loving it but things like this really stump me sometimes! LOL
The generalisation I guess is how to convert an array of bytes into a number where each byte must be multiplied by its respective power... I am still working on it myself... I am still happy that I used "liftM" for the first time and actually understood why, how and what I was doing it for LMAO
Thanks... I have a feeling the answers will really help me.
Sean Charles.