
Hello all, I'm wondering if anyone has a reference to any binary IO and data conversion tutorials. I'm playing around with generating a BMP file in haskell, and am a little stuck on the "best" way to go about the simple task of creating the BMP header. The header is "BM" + 4 bytes for file size + 4 bytes reserved + 4 bytes for offset where data begins. I have the basis starting off at: bmpHeader = B.pack $ [ 0x42, 0x4D ] ++ [0 , 0, 0, 0] ++ [0 , 0, 0, 0] ++ [14 :: Int32] (where B is Data.ByteString) I'm wondering how I can: 1/ convert a 32 bit number (Int32, Char32) to 4 Char8 elements 2/ rotate bits/bytes in a 32 bit Char32 (or Int32) so they are explicitly little-endian (I work on a mac powerbook, and it is big-endian) 3/ convert an Integer or Int type to an Int32 type Any pointers or suggestions would be helpful. Thanks -- 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.

On Jan 20, 2008, at 2:26 , Jamie Love wrote:
I'm wondering if anyone has a reference to any binary IO and data conversion tutorials.
You want the binary package: http://hackage.haskell.org/cgi-bin/ hackage-scripts/package/binary-0.4.1 -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

jamie.love:
Hello all,
I'm wondering if anyone has a reference to any binary IO and data conversion tutorials.
A good place to start looking is Data.Binary, http://hackage.haskell.org/cgi-bin/hackage-scripts/package/binary
I'm playing around with generating a BMP file in haskell, and am a little stuck on the "best" way to go about the simple task of creating the BMP header. The header is
"BM" + 4 bytes for file size + 4 bytes reserved + 4 bytes for offset where data begins.
I have the basis starting off at:
bmpHeader = runPut $ [ 0x42, 0x4D ] ++ [0 , 0, 0, 0] ++ [0 , 0, 0, 0] ++ [14 :: Int32]
bmpHeader = runPut $ do put 'B' put 'M' put (0 :: Int32) put (0 :: Int32) put (14 :: Int32) Yields the lazy bytestring, "BM\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\SO"
(where B is Data.ByteString)
I'm wondering how I can:
1/ convert a 32 bit number (Int32, Char32) to 4 Char8 elements
Data.Binary.put (x :: Int32) etc.
2/ rotate bits/bytes in a 32 bit Char32 (or Int32) so they are explicitly little-endian (I work on a mac powerbook, and it is big-endian)
Use the little endian 'put' primitives, putWord32le (fromIntegral (7 :: Int32))
3/ convert an Integer or Int type to an Int32 type
Any pointers or suggestions would be helpful.
fromIntegral Data.Binary should support all this nicely, I hope. -- Don

Ah, thanks Don, Brandon, I looked at this but neglected to read through and understand the example enough. Thanks for the tips, they're a great help. Don Stewart wrote:
jamie.love:
bmpHeader = runPut $ do put 'B' put 'M' put (0 :: Int32) put (0 :: Int32) put (14 :: Int32)
Yields the lazy bytestring,
"BM\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\SO"
-- 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 (3)
-
Brandon S. Allbery KF8NH
-
Don Stewart
-
Jamie Love