Read file/stream

Steven Schveighoffer schveiguy at yahoo.com
Fri Mar 11 10:46:08 PST 2011


On Fri, 11 Mar 2011 13:43:19 -0500, nrgyzer <nrgyzer at gmail.com> wrote:

> I'm trying to read a png file and I'm having some trouble with the
> chunk-size. Each chunk of a png file begins with a 4 byte (unsigned)
> integer. When I read this 4 byte integer (uint) I get an absolutely
> incorrect length. My code currently looks like:
>
> void main(string args) {
>
>    File f = new File("test.png", FileMode.In);
>
>    // png signature
>    ubyte[8] buffer;
>    f.read(buffer);
>
>    // first chunk (IHDR)
>    uint size;
>    f.read(size);
>
>    f.close();
> }
>
> When I run my code, I get 218103808 instead of 13 (decimal) or 0x0D
> (hex). When I try to read the 4 byte integer as a ubyte[4]-array, I
> get [0, 0, 0, 13] where 13 seems to be the correct ones because my
> hex-editor says [0x00 0x00 0x00 0x0D] for these 4 bytes.
>
> I hope anyone know where my mistake is. Thanks!

http://en.wikipedia.org/wiki/Endianness

Intel boxes are Little endian, which means the correct 4-byte data should  
be [13, 0, 0, 0].

I am not sure what facilities Phobos provides for reading/writing integers  
in network order (i.e. Big Endian), but I'm sure there's something.

-Steve


More information about the Digitalmars-d-learn mailing list