What function, or series of functions, can convert an Int32 to an Int in Haskell? I know that HashTable.hashString can convert to an Int32, but I need a function that can convert the other way.
ghci> import Data.Int
ghci> fromIntegral (5 :: Int32) :: Int
5
ghci> fromIntegral (5 :: Int) :: Int32
5
Stop. Hoogle time!...Sadly, Hoogle fails to answer the question well this time. Skipping to the answer...
fromIntegral :: (Integral a, Num b) => a -> b
(docs at hackage.haskell.org)
Note the type signature of fromIntegral
indicates that it will turn any Integral
into any Num
. Since Int32
has a declared instance of Integral
, and Int
has a declared instance of Num
, the fromIntegral
function will do the job.
fromIntegral
will convert from Int32 to Int or the other way around.
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