// This program uses pointers and pointer arithmetics in C to determine // if the program is running on a machine with a network byte ordering // (big endian) or intel byte ordering (little endian). #include int ordering() { unsigned int x = 1; return *((char*)&x+3); } // returns 1 if "big endian" and 0 if "little endian" int main() { if (ordering()) printf("network byte ordering\n"); else printf("intel byte ordering\n"); exit(0); } // function to switch the byte ordering of a 32bit integer: int switchorder(int x) { unsigned char A[4]; A[3] = *((unsigned char*)x); A[2] = *((unsigned char*)x+1); A[1] = *((unsigned char*)x+2); A[0] = *((unsigned char*)x+3); return *((int*)A); } // Conceptually speaking, this program uses a non-type safe feature, // but in this style of programming we are concerned about the format // of data at a low level, and not about what the data represents at // the application level.