Archived

This topic is now archived and is closed to further replies.

Bigendian Vs Littleendian

This topic is 5306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Anyone know of a way to tell if a compiler you are using is big or little ended ? I am reading a lump of data into a structure, and have finally figured out that it''s all stored in reverse to what is in the file. (if I do fs.read(..)) if I do fs>>var1, then fs>>var2 etc, all the data is read in correctly, but each variable is in reverse to what I would expect from looking at the file. Any comments ? Bp.

Share this post


Link to post
Share on other sites

int a = 1;
if(*(char*)&a)
little_endian = true;

If you''re on a little endian system, and reading big endian (aka network byte order) data, use ntoh?.

You can also read byte for byte, constructing the number yourself:

static inline u32 read32(const u8* p)
{
u32 t = 0;
for(int i = 0; i < 32; i += 8)
t |= (*p++) << i;

return t;
}

Share this post


Link to post
Share on other sites