Re: [Haskell-cafe] Basic binary IO

2008-01-20 Thread Jamie Love

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.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Basic binary IO

2008-01-19 Thread Jamie Love

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.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Basic binary IO

2008-01-19 Thread Don Stewart
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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Basic Binary IO

2006-11-01 Thread Nuno Pinto


Hi all!

Todayi was reading System.IO and didn't manage to understand how it works just by reading it.
I looked the internet for some help on this, but only "advanced" information is available.
Can anyone show me how to use openBinaryFile ?
Just an example, like opening file "somefile" and separating it into something that can be edited in the code (like 8 bit words)then go to word nr12 and editthe last bit?

Thanks!
NPSearch from any Web page with powerful protection. Get the FREE Windows Live Toolbar Today! Try it now!
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Basic Binary IO

2006-11-01 Thread Donald Bruce Stewart
nuno:
 
Hi all!
 
Today i was reading System.IO and didn't manage to
understand how it works just by reading it.
I looked the internet for some help on this, but only
advanced information is available.
Can anyone show me how to use openBinaryFile ?
Just an example, like opening file somefile and separating
it into something that can be edited in the code (like 8 bit
words) then go to word nr12 and edit the last bit?

http://haskell.org/haskellwiki/Binary_IO

For flat lists of bytes, use Data.ByteString, for structured data, try
NewBinary.

There are other options too, documented above.

openBinaryFile just sets the line ending handling on windows. I don't
think it does what you think it does.

-- Don
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re[2]: [Haskell-cafe] Basic Binary IO

2006-11-01 Thread Bulat Ziganshin
Hello Donald,

Thursday, November 2, 2006, 4:31:31 AM, you wrote:
Just an example, like opening file somefile and separating
it into something that can be edited in the code (like 8 bit
words) then go to word nr12 and edit the last bit?

 http://haskell.org/haskellwiki/Binary_IO

 For flat lists of bytes, use Data.ByteString, for structured data, try
 NewBinary.


you can also use hGetBuf and pointers machinery


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe