Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell function seems to be limiting integer length - i thought it used bignums?

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
like image 258
Chris Avatar asked Jul 16 '10 02:07

Chris


1 Answers

Your problem is that ord returns an Int, which is fixed-width. You want toInteger $ ord h.

like image 93
Chuck Avatar answered Sep 19 '22 07:09

Chuck