On Tue, 29 Jul 2014 01:37:30 +0200
Staven <pvl.staven_AT_gmail.com> wrote:
> Isn't ntohl superfluous here? In fact, wouldn't this break on a big-endian
> architecture? If I'm reading this right, we're storing the value in memory in
> reverse (of native) byte order, assume it's now big-endian and convert it to
> native byte order. Wouldn't this suffice?
> width = (hdr[9] << 24) | (hdr[10] << 16) | (hdr[11] << 8) | hdr[12]
> height = (hdr[13] << 24) | (hdr[14] << 16) | (hdr[15] << 8) | hdr[16]
No.
Be careful: the n in ntohl does not stand for native, but network.
More or less by convention, the Network Byte Order today is BE. What
ntohl makes sure is that in case we are on a LE-system, it's arranged
properly.
ntohl is only superfluous here when we are on a BE-machine. Because
that's when network- and host-byte-order are the same.
In any other case, it is mandatory.
Don't get confused by the name of ntohl() and look at it with the
identical be32toh(). The only reason I used ntohl() and htonl() here is
the fact they're POSIX, and endian.h isn't.
Cheers
FRIGN
--
FRIGN <dev_AT_frign.de>
Received on Tue Jul 29 2014 - 01:59:32 CEST