i've got a short haskell function here that is supposed to convert "ABCDEF" into 0x41,0x42,0x43,0x44,0x45,0x46 (their ascii values), then multiply them so it becomes 0x4142,4344,4546 but it seems to be limiting integer length - i thought haskell used arbitrary bignums?
The last line of the code works fine, which puzzles me
Any ideas? Thanks a lot
import Data.Char
import Numeric
strToHex2 (h:[]) = ord h
strToHex2 (h:t) = (ord h) + ((strToHex2 t) * 256)
strToHex s = strToHex2 (reverse s)
main = do
print(strToHex "ABCDEF")
print ((((((((0x41*256+0x42)*256)+0x43)*256)+0x44)*256)+0x45)*256+0x46)
The output is:
1128547654 <- limited to 32 bits for some reason?
71752852194630 <- that's fine
Your problem is that ord
returns an Int
, which is fixed-width. You want toInteger $ ord h
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With