byteswap below does what I want, but I fear it is inefficient for larger chunks of binary data. Is there an efficient library function or something I can use?
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString as B (ByteString, length, append, cons, foldl)
byteswap :: B.ByteString -> B.ByteString
byteswap = let
swapper (collector, result) byte = let
updated = B.cons byte collector
in if 3 < B.length updated then ("", B.append result updated) else (updated, result)
in snd . B.foldl swapper ("", "")
main = print $ byteswap "1234abcdXYZ"
Prints 4321dcba.