Is there some ready-made function that converts Word32 and Word64 between big/little/host-endian representations? As pointed out in the comments, this shouldn't be needed if (de)serialization is performed correctly, but could be handy in some specific situations when dealing with low-level code.
I found the following solutions, neither perfect:
Use byteorder to determine the current host order, and if little-endian, use
byteSwap32on each word (or alternatively the one from base-compat.case byteOrder of LittleEndian -> byteSwap32 _ -> idSerialize the words with cereal's
putWord32beand immediately read them withgetWord32host(or alternativlyputWord32hostand read withgetWord32be). This adds somewhat more heavy-weight dependency, but gives more flexibility for conversions to other formats.either (error "Unexpected error when converting ip address") id . runGet getWord32host . runPut . putWord32beImport the native functions:
foreign import ccall unsafe "htonl" htonl :: Word32 -> Word32 foreign import ccall unsafe "ntohl" ntohl :: Word32 -> Word32
Is there anything better or more convenient?
HostAddress6stores data in itsWord32in such a way that programs can observe the difference depending on the architecture. After examining the source code, parsingstruct in6_addris actually not host dependent. So for using network this shouldn't be needed at all. While my question still applies, in correct scenarios where (de)serialization is done correctly, it shouldn't be needed. - Petr