If intToNat is a blunt instrument, then fromIntegral is the rapier :-)  Remember Word 
is GHC-specific; the FFI doesn't define it.

Simon

> -----Original Message-----
> From: Julian Seward (Intl Vendor) [mailto:[EMAIL PROTECTED]] 
> Sent: 25 February 2002 16:57
> To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
> Subject: RE: Straightforward conversion from Int <-> Word
> 
> 
> 
> I've found this annoying in the past, and have resorted to hitting
> it on the head (GHC-specifically) with a blunt instrument:
> 
>    intToNat :: Int -> Word
>    intToNat (I# i#) = W# (unsafeCast# i#)
> 
> This probably defeats the good intentions of the FFI designers,
> but it kinda seems like it'll work on all reasonable architectures.
> 
> J
> 
> | -----Original Message-----
> | From: Jan-Willem Maessen [mailto:[EMAIL PROTECTED]] 
> | Sent: Monday, February 25, 2002 4:46 PM
> | To: [EMAIL PROTECTED]
> | Subject: Straightforward conversion from Int <-> Word
> | 
> | 
> | I've cast about a bit and haven't been able to find the appropriate
> | functionality, so I'm asking here.
> | 
> | I'd like to convert Int32 to and from Word32 in the good 
> old-fashioned
> | bitwise fashion (perserving modular arithmetic).  I have 
> the following
> | code to show what I mean:
> | 
> |   intToNat i
> |     | i >= 0    = fromIntegral i
> |     | otherwise = fromIntegral (i - minBound) + negBound
> |     where negBound = fromInteger . negate . toInteger $ 
> | (minBound :: Int)
> | 
> | One might think that "intToWord32" and "word32ToInt" in lang/Word
> | would do what I want---but no!  These are depracated, and I must use
> | fromIntegral, which will only perform the conversion 
> correctly for the
> | portions of the numeric range which overlap.
> | 
> | How do I do the straightforward conversion module 2^32?  
> I'm sure it's
> | buried in there somewhere...  I can't even turn up an 
> appropriate type
> | signature, though.
> | 
> | -Jan-Willem Maessen
> | 
> | _______________________________________________
> | Glasgow-haskell-users mailing list
> | [EMAIL PROTECTED]
> | http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
> | 
> _______________________________________________
> Glasgow-haskell-users mailing list
> [EMAIL PROTECTED]
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
> 
_______________________________________________
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

Reply via email to