Read file/stream

Simen kjaeraas simen.kjaras at gmail.com
Fri Mar 11 10:46:34 PST 2011


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!

Looks to be an endian issue. 0x0000_000D is 218,103,808 in decimal
in little-endian (Intel), and 13 in big-endian (Motorola).

-- 
Simen


More information about the Digitalmars-d-learn mailing list