Bigendian Vs Littleendian

Started by
0 comments, last by Bagpuss 20 years, 10 months ago
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.
Advertisement
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;} 
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3

This topic is closed to new replies.

Advertisement