Binary IO of a list of ints

Hi there I have a list of ints, with values between 0 and 255 and I need to print them out in little endian form to a file. I've been using Data.Binary for single values, e.g. runPut $ do put 'B' put (0 :: Int32) I'm wondering how I can go about writing a list of Ints out. My thought was to do something like: foldr (\x B.hPut output (runPut $ do put (x :: Word8))) data (where output is my file handle), but apart from giving me type errors, it seems a rather arduous way to do it. Could anyone suggest a better way to do this? -- Jamie Love Senior Consultant Aviarc Australia Mobile: +61 400 548 048 ------------------------------------------------------------ This message has been scanned for viruses and dangerous content by MailScanner and is believed to be clean.

Hi, On 2008-01-24 12:14, Jamie Love wrote:
I have a list of ints, with values between 0 and 255 and I need to print them out in little endian form to a file.
How about just using Data.Char.chr ? Prelude> let a = [32..64] :: [Int] Prelude> map Data.Char.chr a " !\"#$%&'()*+,-./0123456789:;<=>?@" --Stephan

Stephan Walter wrote:
Hi,
On 2008-01-24 12:14, Jamie Love wrote:
I have a list of ints, with values between 0 and 255 and I need to print them out in little endian form to a file.
How about just using Data.Char.chr ?
Essentially because I need to control the byte ordering, and it has to be in the current case the opposite to my computer's native ordering (the file is a binary file).
Prelude> let a = [32..64] :: [Int] Prelude> map Data.Char.chr a " !\"#$%&'()*+,-./0123456789:;<=>?@"
--Stephan
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
------------------------------------------------------------
This message has been scanned for viruses and dangerous content by MailScanner and is believed to be clean.
-- Jamie Love Senior Consultant Aviarc Australia Mobile: +61 400 548 048 ------------------------------------------------------------ This message has been scanned for viruses and dangerous content by MailScanner and is believed to be clean.

(untested)
mapM_ runPut data
?
On Jan 24, 2008 12:14 PM, Jamie Love
Hi there
I have a list of ints, with values between 0 and 255 and I need to print them out in little endian form to a file.
I've been using Data.Binary for single values, e.g.
runPut $ do put 'B' put (0 :: Int32)
I'm wondering how I can go about writing a list of Ints out. My thought was to do something like:
foldr (\x B.hPut output (runPut $ do put (x :: Word8))) data
(where output is my file handle), but apart from giving me type errors, it seems a rather arduous way to do it.
Could anyone suggest a better way to do this?
-- Jamie Love Senior Consultant Aviarc Australia Mobile: +61 400 548 048
------------------------------------------------------------
This message has been scanned for viruses and dangerous content by MailScanner and is believed to be clean.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 24 Jan 2008, jamie.love@aviarc.com.au wrote:
Hi there
I have a list of ints, with values between 0 and 255 and I need to print them out in little endian form to a file.
I've been using Data.Binary for single values, e.g.
runPut $ do put 'B' put (0 :: Int32)
This will be big endian by default. If it needs to be little endian, use Data.Binary.Put.putWord32le and relatives. With a list, you could do something like: runPut $ mapM_ (putWord32le . fromIntegral) listOfInts I hope this helps. Jed

Thanks Jed, That works (or at least it's taking ages to error :-) ) Jed Brown wrote:
On 24 Jan 2008, jamie.love@aviarc.com.au wrote:
Hi there
I have a list of ints, with values between 0 and 255 and I need to print them out in little endian form to a file.
I've been using Data.Binary for single values, e.g.
runPut $ do put 'B' put (0 :: Int32)
This will be big endian by default. If it needs to be little endian, use Data.Binary.Put.putWord32le and relatives. With a list, you could do something like:
runPut $ mapM_ (putWord32le . fromIntegral) listOfInts
I hope this helps.
Jed
-- Jamie Love Senior Consultant Aviarc Australia Mobile: +61 400 548 048 ------------------------------------------------------------ This message has been scanned for viruses and dangerous content by MailScanner and is believed to be clean.
participants (4)
-
Fraser Wilson
-
Jamie Love
-
Jed Brown
-
Stephan Walter