[Haskell-cafe] Detecting system endianness

Ryan Ingram ryani.spam at gmail.com
Thu Dec 18 07:40:47 EST 2008


Actually, this is probably safer:

import Foreign.Marshal.Alloc
import Foreign.Ptr
import Foreign.Storable
import Data.Word
import System.IO.Unsafe

endianCheck = unsafePerformIO $ alloca $ \p -> poke p (0x01020304 ::
Word32) >> peek (castPtr p :: Ptr Word8)

littleEndian = endianCheck == 4
bigEndian = endianCheck == 1

  -- ryan

On Thu, Dec 18, 2008 at 4:33 AM, Ryan Ingram <ryani.spam at gmail.com> wrote:
> I think something like this might work:
>
> Prelude GHC.Exts GHC.Word> let W64# x = 0x100000002 in W32# (unsafeCoerce# x)
> 2
>
> You should get 1 for big-endian and 2 for little-endian.
>
> (Disclaimer: not particularily well-tested.)
>
>  -- ryan
>
> On Thu, Dec 18, 2008 at 3:27 AM, Mauricio <briqueabraque at yahoo.com> wrote:
>> Hi,
>>
>> Is there some way I can check the endianness
>> of the machine my haskell code is running in?
>>
>> Thanks,
>> Maurício
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>


More information about the Haskell-Cafe mailing list