2010-07-16 21 views
5

我在這裏有一個短的haskell函數,它應該將「ABCDEF」轉換爲0x41,0x42,0x43,0x44,0x45,0x46(它們的ascii值),然後將它們相乘,使它變爲0x4142,4344,4546,但它似乎是限制整數長度 - 我認爲haskell使用任意的bignums?Haskell函數似乎是限制整數長度 - 我認爲它使用了bignums?

代碼的最後一行正常工作,這讓我爲難,

任何想法?非常感謝

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) 

的輸出是:

1128547654  <- limited to 32 bits for some reason? 
71752852194630 <- that's fine 
+1

參見http://stackoverflow.com/questions/3429291/haskell-int-and-integer – 2011-04-22 17:30:33

+0

類型是你的朋友。添加類型簽名,並且清楚發生了什麼事情! – 2017-04-06 11:34:15

回答

10

您的問題是ord返回Int,這是固定寬度。你想要toInteger $ ord h

+1

因此,Int和Integer之間有區別嗎? – Chris 2010-07-16 02:59:14

+2

@Chris是的,請參閱http://www.haskell.org/tutorial/numbers.html – chollida 2010-07-16 03:05:48

相關問題